

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/blog/img/fluid.png">
  <link rel="icon" href="/blog/images/base.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Airfe">
  <meta name="keywords" content="">
  
    <meta name="description" content="本文介绍了Vue的基础知识。">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue基础知识">
<meta property="og:url" content="https://airfe.gitee.io/blog/2023/06/22/Vue%E5%9F%BA%E7%A1%80/index.html">
<meta property="og:site_name" content="Airfe的博客">
<meta property="og:description" content="本文介绍了Vue的基础知识。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://markdown-1307246189.cos.ap-chongqing.myqcloud.com/pageImage/Vue%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86.png">
<meta property="article:published_time" content="2023-06-22T00:00:00.000Z">
<meta property="article:modified_time" content="2024-02-26T12:23:18.185Z">
<meta property="article:author" content="Airfe">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://markdown-1307246189.cos.ap-chongqing.myqcloud.com/pageImage/Vue%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86.png">
  
  
  
    <meta name="referrer" content="no-referrer" />
  
  <title>Vue基础知识 - Airfe的博客</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"airfe.gitee.io","root":"/blog/","version":"1.9.4","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":"699a5d71ac7179b23874a59b0666a6f6","google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":"RW68dMkEpeIOJJiBr1lPkhOS-gzGzoHsz","app_key":"P5fzMTGSJIoTaGvmgzsUHeNZ","server_url":"https://rw68dmke.lc-cn-n1-shared.com","path":"window.location.pathname","ignore_local":false}},"search_path":"/blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/blog/js/utils.js" ></script>
  <script  src="/blog/js/color-schema.js" ></script>
  

  
    <!-- Baidu Analytics -->
    <script async>
      if (!Fluid.ctx.dnt) {
        var _hmt = _hmt || [];
        (function() {
          var hm = document.createElement("script");
          hm.src = "https://hm.baidu.com/hm.js?699a5d71ac7179b23874a59b0666a6f6";
          var s = document.getElementsByTagName("script")[0];
          s.parentNode.insertBefore(hm, s);
        })();
      }
    </script>
  

  

  

  

  

  

  
    
  



  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/blog/">
      <strong>Airfe</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/links/">
                <i class="iconfont icon-link-fill"></i>
                <span>友链</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/blog/images/bg.jpg') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Vue基础知识"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-06-22 08:00" pubdate>
          2023年06月22日
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          33k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          275 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Vue基础知识</h1>
            
            
              <div class="markdown-body">
                
                <h1 id="一、Vue基础"><a href="#一、Vue基础" class="headerlink" title="一、Vue基础"></a>一、Vue基础</h1><h2 id="1-1-模板语法"><a href="#1-1-模板语法" class="headerlink" title="1.1 模板语法"></a>1.1 模板语法</h2><p>Vue模板语法包括两大类</p>
<ol>
<li><p><strong>差值语法</strong></p>
<p>功能: 用于解析标签体内容。</p>
<p>写法: ， xxx是<strong>js表达式</strong>。且可以直接读取到data中的所有属性。</p>
</li>
<li><p><strong>指令语法</strong></p>
<p>功能: 用于解析标签(包括：标签属性、标签体内容、绑定事件…..)。</p>
<p>举例:<code>&lt;a v-bind:href=&quot;xxx&quot;&gt; </code>或简写为<code> &lt;a :href=&quot;xxx&quot;&gt;</code>，xxx同样要写<strong>js表达式</strong>，</p>
<p>且可以直接读取到data中的所有属性。<code>xxx</code>解析显示的是去引号之后的值。</p>
<blockquote>
<p>Vue中有很多的指令，且形式都是<code>v-？？？</code>,此处只是拿<code>v-bind</code>举例。</p>
</blockquote>
</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;!--插值语法--&gt;<br>&lt;h2&gt;&#123;&#123;firstName + &#x27; &#x27; + lastName&#125;&#125;&lt;/h2&gt;<br>&lt;!--指定语法--&gt;<br>//动态指定元素标签值<br>&lt;a :href=&quot;url&quot;&gt;点我去百度学习&lt;/a&gt; <br>//动态设置元素标签同时多个值的状态<br>&lt;h2 :class=&quot;&#123;active:isActive, line:isline&#125;&quot;&gt;点我去百度学习&lt;/h2&gt;<br>//根据表达式动态指定标签值<br>&lt;div :class=&quot;leftOrRight === &#x27;left&#x27; ? &#x27;left&#x27; : &#x27;right&#x27;&quot;&gt;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>标签里的标签属性不能用差值语法来动态指定，若要动态设置标签属性用指令语法中的v-bind（语法糖为冒号：）</p>
</blockquote>
<h2 id="1-2-数据绑定"><a href="#1-2-数据绑定" class="headerlink" title="1.2 数据绑定"></a>1.2 数据绑定</h2><p><code>Vue</code>中有两种数据绑定的方式</p>
<ol>
<li><p><strong>单向绑定</strong><code>v-bind</code></p>
<p>只能从data流向页面</p>
</li>
<li><p><strong>双向绑定</strong><code>v-model</code></p>
<p>不仅能从data流向页面，还可以页面流向data</p>
<blockquote>
<p>双向绑定一般都应用在表单类元素上，如<code>&lt;input&gt;``&lt;select&gt;``&lt;textarea&gt;</code></p>
<p>v-model:value可以简写为v-model因为v-model默认收集的就是value 值</p>
</blockquote>
</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs vue">单向数据绑定: &lt;input type=&quot;text&quot; :value= ”name&quot;&gt;&lt;br/&gt;<br>双向数据绑定: &lt;input type=&quot;text&quot; v-model=&quot;name&quot;&gt;&lt;br/&gt;<br></code></pre></td></tr></table></figure>

<h2 id="1-3-事件处理"><a href="#1-3-事件处理" class="headerlink" title="1.3 事件处理"></a>1.3 事件处理</h2><h3 id="1-3-1-事件的基本用法"><a href="#1-3-1-事件的基本用法" class="headerlink" title="1.3.1 事件的基本用法"></a>1.3.1 事件的基本用法</h3><ol>
<li><p>&#x3D;&#x3D;使用<code>v-on:xxx</code>或<code>@xx</code>绑定事件，其中xxx是事件名&#x3D;&#x3D;</p>
</li>
<li><p>事件的回调需要配置在<code>methods </code>对象中，最终会在<code>vm</code>上</p>
</li>
<li><p><code>methods </code>中配置的函数,<code>不要用箭头函数</code>，否则this就不是<code>vm</code>了</p>
</li>
<li><p><code>methods</code> 中配置的函数，都是被<code>Vue </code>所管理的函数，this 的指向是<code>vm</code>或<code>组件实例对象</code></p>
</li>
<li><p><code>@c1ick=&quot; demo&quot;</code>和<code>@c1ick=&quot; demo($event)&quot;</code>效果一致，但后者可以传参</p>
</li>
</ol>
<h3 id="1-3-2-事件修饰符"><a href="#1-3-2-事件修饰符" class="headerlink" title="1.3.2 事件修饰符"></a>1.3.2 事件修饰符</h3><p>Vue中的事件修饰符:</p>
<ol>
<li><strong>prevent</strong>:    阻止默认事件(常用)</li>
<li><strong>stop</strong>:          阻止事件冒泡(常用)</li>
<li><strong>once</strong>:         事件只触发一次(常用) </li>
<li>capture:    使用事件的捕获(与冒泡顺序相反)模式</li>
<li>self:            只有event.target是当前操作的元素时才触发事件</li>
<li>passive:     事件的默认行为立即执行，无需等待事件回调执行完毕</li>
</ol>
<blockquote>
<p>修饰符可以连续写，比如可以这么用: @click.prevent.stop&#x3D;” showInfo”</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;!-- 阻止事件冒泡(常用) --&gt;<br>&lt;div class=&quot;demo1&quot; @click=&quot;showInfo&quot; &gt;<br>&lt;button @click.stop=&quot;showInfo&quot;&gt;点我提示信息&lt;/button&gt;<br>&lt;/div&gt;<br>&lt;!-- 事件只触发一次(常用) --&gt;<br>&lt;button @click.once=&quot;showInfo&quot;&gt;点我提示信息&lt;/button&gt;<br>&lt;!--使用事件的捕获模式--&gt;<br>&lt;div class= &quot;box1&quot; @click.capture=&quot;showMsg(1)&quot;&gt;<br>    div1<br>    &lt;div class=&quot;box2&quot; @click=&quot;showMsg(2)&quot;&gt;<br>        div2<br>    &lt;/div&gt;<br>&lt;/div&gt;<br>&lt;!--只有event.target是当前操作的元素时才触发事件; --&gt;<br>&lt;div class=&quot;demo1&quot; @click.self=&quot; showInfo&quot;&gt;<br>&lt;button @click=&quot;showInfo&quot; &gt;点我提示信息&lt;/ button&gt;<br>&lt;/div&gt;<br>&lt;!--事件的默认行为立即执行，无需等待事件回调执行完毕; - - &gt;<br>&lt;!-- scrol1是滚动条滚动，passsive没有影响--&gt;<br>&lt;!-- whee1是鼠标滚轮滚动，passive有影响--&gt;<br>&lt;ul @wheel.passive=&quot;demo&quot; class=&quot;list&quot;&gt;<br>    &lt;1i&gt;1&lt;/1i&gt;<br>    &lt;li&gt;2&lt;/li&gt;<br>    &lt;li&gt;3&lt;/li&gt;<br>    &lt;li&gt;4&lt;/li&gt;<br>&lt;/ul&gt;<br></code></pre></td></tr></table></figure>

<h3 id="1-3-2-鼠标事件"><a href="#1-3-2-鼠标事件" class="headerlink" title="1.3.2 鼠标事件"></a>1.3.2 鼠标事件</h3><ul>
<li><p>单击事件&#x2F;双击事件 <code>@click() / @dblclick()</code></p>
</li>
<li><p>按下触发&#x2F;抬起触发  <code>@mousedown() / @mouseup()</code></p>
</li>
<li><p>当鼠标指针在<code>指定的元素中移动时</code>，就会发生 mousemove 事件移动一个像素，就会发生一次 <code>mousemove  @mousemove()</code></p>
</li>
<li><p>当鼠标指针离开被选元素时，会发生 mouseleave 事件   <code>@mouseleave()</code></p>
</li>
<li><p>当鼠标指针从元素上移开时，发生 mouseout 事件          <code>@mouseout()</code></p>
<blockquote>
<p>与 mouseleave 事件不同，不论鼠标指针离开<code>被选元素还是任何子元素，</code>都会触发 mouseout 事件。</p>
</blockquote>
</li>
<li><p>当鼠标指针进入<code>被选元素时，</code>会发生 mouseenter 事件   <code>@mouseenter()</code></p>
</li>
<li><p>当鼠标指针位于元素上方时，会发生 mouseover 事件   <code>@mouseover()</code></p>
<blockquote>
<p>注意：与 mouseenter 事件不同，mouseover 事件在鼠标指针进入<code>被选元素或任意子元素时</code>都会被触发，mouseenter 事件只有在鼠标指针进入<code>被选元素</code>时被触发。</p>
</blockquote>
</li>
</ul>
<h3 id="1-3-3-键盘事件"><a href="#1-3-3-键盘事件" class="headerlink" title="1.3.3 键盘事件"></a>1.3.3 键盘事件</h3><blockquote>
<p>键盘上的每个按键都有自己的名称和编码，例如: Enter (13) 。而Vue还对一些常用按键起了别名方便使用</p>
</blockquote>
<ul>
<li><p>Vue中常用的按键别名</p>
<ul>
<li>回车<code>enter</code></li>
<li>删除<code>delete</code>捕获“删除”和“退格”键</li>
<li>退出<code>esc</code></li>
<li>空格<code>space</code></li>
<li>换行<code>tab</code>特殊，必须配合<code>keydown </code>使用</li>
<li>上<code>up</code></li>
<li>下| <code>down</code></li>
<li>左<code>left</code></li>
<li>右<code>right</code></li>
</ul>
</li>
<li><p>Vue 未提供别名的按键，可以使用按键原始的key值去绑定，但注意要转为<code>kebab-case</code>(多单词小写,短横线写法)</p>
</li>
<li><p>系统修饰键(用法特殊)，ctrl alt shift meta(meta就是win键)</p>
<ul>
<li>配合<code>keyup </code>使用:按下修饰键的同时，再按下其他键，随后释放其他键，事件才被触发指定ctr+y使用<code>@keyup.ctr.y </code></li>
<li>配合<code>keydown</code> 使用：正常触发事件</li>
</ul>
</li>
<li><p><strong>Vue.config.keyCodes.自定义键名</strong> &#x3D; 键码，可以去定制按键别名</p>
</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;root&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>欢迎打开&#123;&#123;name&#125;&#125;笔记<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">&quot;按下回车提示输入”@keyup.enter=&quot;</span><span class="hljs-attr">showInfo</span>&quot;&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">&quot;按下tab提示输入”@keydown.tab=&quot;</span><span class="hljs-attr">showInfo</span>&quot;&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">&quot;按下回车提示输入”@keydown.huiche=&quot;</span> <span class="hljs-attr">showInfo</span>&quot;&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>                                                                        <br><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span>&gt;</span><span class="language-javascript"> </span><br><span class="language-javascript">    <span class="hljs-title class_">Vue</span>.<span class="hljs-property">config</span>.<span class="hljs-property">productionTip</span> = <span class="hljs-literal">false</span><span class="hljs-comment">//阻止vue: 在启动时生成生产提示。</span></span><br><span class="language-javascript">    <span class="hljs-title class_">Vue</span>.<span class="hljs-property">config</span>.<span class="hljs-property">keyCodes</span>.<span class="hljs-property">huiche</span> = <span class="hljs-number">13</span><span class="hljs-comment">//定义了一个别名按键</span></span><br><span class="language-javascript">    <span class="hljs-keyword">new</span> <span class="hljs-title class_">Vue</span>(&#123;</span><br><span class="language-javascript">        <span class="hljs-attr">el</span>: <span class="hljs-string">&quot;#root&quot;</span>,</span><br><span class="language-javascript">        <span class="hljs-attr">data</span>: &#123;</span><br><span class="language-javascript">            &#125;,</span><br><span class="language-javascript">            <span class="hljs-attr">methods</span>: &#123;</span><br><span class="language-javascript">            <span class="hljs-title function_">showInfo</span>(<span class="hljs-params">e</span>) &#123;</span><br><span class="language-javascript">                <span class="hljs-comment">// console.log(e.key,e.keyCode)</span></span><br><span class="language-javascript">                <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(e.<span class="hljs-property">target</span>.<span class="hljs-property">value</span>)</span><br><span class="language-javascript">                &#125;</span><br><span class="language-javascript">        &#125;)</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="1-4-计算属性和侦听属性"><a href="#1-4-计算属性和侦听属性" class="headerlink" title="1.4 计算属性和侦听属性"></a>1.4 计算属性和侦听属性</h2><h3 id="1-4-1computed计算属性"><a href="#1-4-1computed计算属性" class="headerlink" title="1.4.1computed计算属性"></a>1.4.1<code>computed</code>计算属性</h3><ol>
<li>定义：要用的属性不存在，<code>需要通过已有属性计算得来</code></li>
<li>原理:底层借助了<code>objcet .defineproperty()</code>方法提供的<code>getter</code> 和<code>setter</code></li>
<li><code>get</code>函数什么时候执行?<br>a. 初次读取时会执行一次，并做<strong>缓存</strong><br>b. 当依赖的数据发生改变时会被再次调用</li>
<li>优势：与<code>methods</code>实现相比，<code>内部有缓存机制(复用)</code>，效率更高，调试方便</li>
</ol>
<blockquote>
<p>计算属性最终会出现在vm上，直接读取使用即可</p>
<p>如果计算属性要被修改，那必须写set函数去响应修改，且set中要引起计算时依赖的数据发生改变</p>
<p>如果计算属性确定不考虑修改，可以使用计算属性的简写形式</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs html"> <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;root&quot;</span>&gt;</span><br>     姓：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;firstName&quot;</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>     名：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;lastName&quot;</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>     测试：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;x&quot;</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span> // 这里修改 不会调 fullName的get方法<br>     全名：<span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>&#123;&#123;fullName&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>     <span class="hljs-comment">&lt;!-- 全名：&lt;span&gt;&#123;&#123;fullName&#125;&#125;&lt;/span&gt; &lt;br/&gt; --&gt;</span><br>     <span class="hljs-comment">&lt;!-- 全名：&lt;span&gt;&#123;&#123;fullName&#125;&#125;&lt;/span&gt; &lt;br/&gt; --&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript">     <span class="hljs-title class_">Vue</span>.<span class="hljs-property">config</span>.<span class="hljs-property">productionTip</span> = <span class="hljs-literal">false</span></span><br><span class="language-javascript">     <span class="hljs-keyword">const</span> vm = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Vue</span>(&#123;</span><br><span class="language-javascript">         <span class="hljs-attr">el</span>: <span class="hljs-string">&#x27;#root&#x27;</span>,</span><br><span class="language-javascript">         <span class="hljs-attr">data</span>: &#123;</span><br><span class="language-javascript">             <span class="hljs-attr">firstName</span>:<span class="hljs-string">&#x27;张&#x27;</span>,</span><br><span class="language-javascript">             <span class="hljs-attr">lastName</span>:<span class="hljs-string">&#x27;三&#x27;</span>,</span><br><span class="language-javascript">             <span class="hljs-attr">x</span>:<span class="hljs-string">&#x27;你好&#x27;</span></span><br><span class="language-javascript">         &#125;,</span><br><span class="language-javascript">         <span class="hljs-attr">computed</span>: &#123;</span><br><span class="language-javascript">         <span class="hljs-comment">// 完整写法</span></span><br><span class="language-javascript">         <span class="hljs-comment">// fullName: &#123;</span></span><br><span class="language-javascript">         <span class="hljs-comment">// get() &#123;</span></span><br><span class="language-javascript">         <span class="hljs-comment">// console.log(&#x27;get被调用了&#x27;)</span></span><br><span class="language-javascript">         <span class="hljs-comment">// return this.firstName + &#x27;-&#x27; + this.lastName</span></span><br><span class="language-javascript">         <span class="hljs-comment">// &#125;,</span></span><br><span class="language-javascript">         <span class="hljs-comment">// set(value) &#123;</span></span><br><span class="language-javascript">         <span class="hljs-comment">// console.log(&#x27;set&#x27;, value)</span></span><br><span class="language-javascript">         <span class="hljs-comment">// const arr = value.split(&#x27;-&#x27;)</span></span><br><span class="language-javascript">         <span class="hljs-comment">// this.firstName = arr[0]</span></span><br><span class="language-javascript">         <span class="hljs-comment">// this.lastName = arr[1]</span></span><br><span class="language-javascript">         <span class="hljs-comment">// &#125;</span></span><br><span class="language-javascript">         <span class="hljs-comment">// &#125;</span></span><br><span class="language-javascript">         <span class="hljs-comment">// 只读取不修改时可以简写</span></span><br><span class="language-javascript">         <span class="hljs-title function_">fullName</span>(<span class="hljs-params"></span>) &#123;</span><br><span class="language-javascript">             <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;get被调用了&#x27;</span>)</span><br><span class="language-javascript">             <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">firstName</span> + <span class="hljs-string">&#x27;-&#x27;</span> + <span class="hljs-variable language_">this</span>.<span class="hljs-property">lastName</span></span><br><span class="language-javascript">             &#125;</span><br><span class="language-javascript">         &#125;</span><br><span class="language-javascript">     &#125;)</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>计算属性可以被修改，要修改的话就要写setter函数，在setter函数中修改掉其依赖的data中的属性才会真正修改计算属性值。</p>
<p>修改计算属性——&gt;setter——&gt;影响到getter返回值</p>
<h3 id="1-4-2-watch侦听属性"><a href="#1-4-2-watch侦听属性" class="headerlink" title="1.4.2 watch侦听属性"></a>1.4.2 <code>watch</code>侦听属性</h3><ul>
<li>基本属性<ul>
<li>当被监视的属性变化时，回调函数自动调用，进行相关操作 </li>
<li>监视的属性必须存在，才能进行监视，既可以监视 **data中的普通属性 ，也可以监视计算属性 **</li>
<li>配置项属性<strong>immediate:false</strong> ，<strong>改为 true</strong>，则初始化时调用一次 <strong>handler(newValue,oldValue)</strong> </li>
<li>监视有两种写法<ul>
<li>创建 <strong>Vue 时传入 watch: {} 配置</strong> </li>
<li>通过 vm.$watch() 监视</li>
</ul>
</li>
</ul>
</li>
<li>深度侦听<ul>
<li>Vue 中的 watch 默认不监测对象内部值的改变（一层） </li>
<li>在 watch 中配置 deep:true 可以监测对象内部值的改变（多层） </li>
<li>Vue 自身可以监测对象内部值的改变，但 Vue 提供的 watch 默认不可以 </li>
<li>使用 watch 时根据监视数据的具体结构，决定是否采用深度监视</li>
</ul>
</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>天气案例_监视属性_简写<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;../js/vue.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;root&quot;</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>今天天气很&#123;&#123; info &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;changeWeather&quot;</span>&gt;</span>切换天气<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">hr</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>a的值是：&#123;&#123;number.a&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span> <br>     <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;numbers.a++&quot;</span>&gt;</span>点击a+1<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript">     <span class="hljs-title class_">Vue</span>.<span class="hljs-property">config</span>.<span class="hljs-property">productionTip</span> = <span class="hljs-literal">false</span></span><br><span class="language-javascript">     <span class="hljs-keyword">const</span> vm = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Vue</span>(&#123;</span><br><span class="language-javascript">         <span class="hljs-attr">el</span>: <span class="hljs-string">&#x27;#root&#x27;</span>,</span><br><span class="language-javascript">         <span class="hljs-attr">data</span>: &#123;</span><br><span class="language-javascript">             <span class="hljs-attr">isHot</span>: <span class="hljs-literal">true</span>,</span><br><span class="language-javascript">             <span class="hljs-attr">numbers</span>: &#123;</span><br><span class="language-javascript">                 <span class="hljs-attr">a</span>: <span class="hljs-number">1</span>,</span><br><span class="language-javascript">                 <span class="hljs-attr">b</span>: <span class="hljs-number">1</span></span><br><span class="language-javascript">             &#125;</span><br><span class="language-javascript">         &#125;,</span><br><span class="language-javascript">         <span class="hljs-attr">computed</span>: &#123;<span class="hljs-title function_">info</span>(<span class="hljs-params"></span>) &#123;<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">isHot</span> ? <span class="hljs-string">&#x27;炎热&#x27;</span> : <span class="hljs-string">&#x27;凉爽&#x27;</span>&#125;&#125;,</span><br><span class="language-javascript">         <span class="hljs-attr">methods</span>: &#123;<span class="hljs-title function_">changeWeather</span>(<span class="hljs-params"></span>) &#123;<span class="hljs-variable language_">this</span>.<span class="hljs-property">isHot</span> = !<span class="hljs-variable language_">this</span>.<span class="hljs-property">isHot</span>&#125;&#125;,</span><br><span class="language-javascript">         <span class="hljs-attr">watch</span>: &#123;</span><br><span class="language-javascript">             <span class="hljs-comment">// 正常写法</span></span><br><span class="language-javascript">             <span class="hljs-comment">// isHot: &#123;</span></span><br><span class="language-javascript">                 <span class="hljs-comment">// immediate:true, // 初始化时让handler调用一下</span></span><br><span class="language-javascript">                 <span class="hljs-comment">// deep:true, // 深度监视</span></span><br><span class="language-javascript">                 <span class="hljs-comment">// 当isHot发生改变时handler被调用</span></span><br><span class="language-javascript">                 <span class="hljs-comment">// handler(newValue, oldValue) &#123;</span></span><br><span class="language-javascript">             		<span class="hljs-comment">// console.log(&#x27;isHot被修改了&#x27;, newValue, oldValue)</span></span><br><span class="language-javascript">                 <span class="hljs-comment">// &#125;</span></span><br><span class="language-javascript">             <span class="hljs-comment">// &#125;,</span></span><br><span class="language-javascript">             <span class="hljs-comment">// 简写</span></span><br><span class="language-javascript">             <span class="hljs-title function_">isHot</span>(<span class="hljs-params">newValue, oldValue</span>) &#123;</span><br><span class="language-javascript">                 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;isHot被修改了&#x27;</span>, newValue, oldValue, <span class="hljs-variable language_">this</span>)</span><br><span class="language-javascript">             &#125;,</span><br><span class="language-javascript">             <span class="hljs-comment">// 监视多级结构中某个属性的变化</span></span><br><span class="language-javascript">             <span class="hljs-string">&#x27;numbers.a&#x27;</span>: &#123;</span><br><span class="language-javascript">                 <span class="hljs-title function_">handler</span>(<span class="hljs-params"></span>) &#123;</span><br><span class="language-javascript">             		<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;a被修改了&#x27;</span>)</span><br><span class="language-javascript">                 &#125;</span><br><span class="language-javascript">             &#125;,</span><br><span class="language-javascript">             <span class="hljs-comment">// 监视多级结构中所有属性变化</span></span><br><span class="language-javascript">             <span class="hljs-attr">numbers</span>: &#123;</span><br><span class="language-javascript">                 <span class="hljs-attr">deep</span>: <span class="hljs-literal">true</span>,</span><br><span class="language-javascript">                 <span class="hljs-title function_">handler</span>(<span class="hljs-params"></span>) &#123;</span><br><span class="language-javascript">             		<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;numbers被修改了&#x27;</span>)</span><br><span class="language-javascript">                 &#125;</span><br><span class="language-javascript">             &#125;</span><br><span class="language-javascript">         &#125;</span><br><span class="language-javascript">     &#125;)</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h3 id="1-4-3-计算属性VS侦听属性"><a href="#1-4-3-计算属性VS侦听属性" class="headerlink" title="1.4.3 计算属性VS侦听属性"></a>1.4.3 计算属性VS侦听属性</h3><ul>
<li>computed能完成的功能，watch都可以完成。</li>
<li>watch能完成的功能，computed不一定能完成，例如: watch可  进行异步操作。</li>
<li>两个重要的小原则:<ul>
<li>所被Vue管理的函数，最好写成普通函数，这样this的指向才是vm或组件实例对象。</li>
<li>所有不被Vue所管理的函数(&#x3D;&#x3D;定时器的回调函数、ajax的回调函数等&#x3D;&#x3D;)，最好写成&#x3D;&#x3D;箭头函数&#x3D;&#x3D;(没有自身的this)，这样this的指向才是vm或组件实例对象。</li>
</ul>
</li>
</ul>
<p><strong>使用计算属性</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">new</span> <span class="hljs-title class_">Vue</span>(&#123;<br>     <span class="hljs-attr">el</span>:<span class="hljs-string">&#x27;#root&#x27;</span>,<br>     <span class="hljs-attr">data</span>:&#123;<br>         <span class="hljs-attr">firstName</span>:<span class="hljs-string">&#x27;张&#x27;</span>,<br>         <span class="hljs-attr">lastName</span>:<span class="hljs-string">&#x27;三&#x27;</span><br>     &#125;,<br>     <span class="hljs-attr">computed</span>:&#123;<br>         <span class="hljs-title function_">fullName</span>(<span class="hljs-params"></span>)&#123;<br>             <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">firstName</span> + <span class="hljs-string">&#x27;-&#x27;</span> + <span class="hljs-variable language_">this</span>.<span class="hljs-property">lastName</span><br>         &#125;<br>     &#125;<br>&#125;)<br></code></pre></td></tr></table></figure>

<p><strong>使用监听属性</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs vue">new Vue(&#123;<br>     el:&#x27;#root&#x27;,<br>     data:&#123;<br>         firstName:&#x27;张&#x27;,<br>         lastName:&#x27;三&#x27;,<br>         fullName:&#x27;张-三&#x27;<br>     &#125;,<br>     watch:&#123;<br>         firstName(val)&#123;<br>         setTimeout(()=&gt;&#123;<br>             this.fullName = val + &#x27;-&#x27; + this.lastName<br>         &#125;,1000);<br>     &#125;,<br>     lastName(val)&#123;<br>         this.fullName = this.firstName + &#x27;-&#x27; + val<br>     &#125;<br>     &#125;<br>&#125;)<br></code></pre></td></tr></table></figure>

<h2 id="1-5-绑定样式和条件渲染"><a href="#1-5-绑定样式和条件渲染" class="headerlink" title="1.5 绑定样式和条件渲染"></a>1.5 绑定样式和条件渲染</h2><h3 id="1-5-1-绑定class样式"><a href="#1-5-1-绑定class样式" class="headerlink" title="1.5.1 绑定class样式"></a>1.5.1 绑定class样式</h3><blockquote>
<p>写法:  <code>:class=&quot;xxx&quot;</code>xxx可以是字符串、数组、对象</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs html"> <span class="hljs-comment">&lt;!-- 绑定class样式--字符串写法，适用于：样式的类名不确定，需要动态指定类名 --&gt;</span><br> <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;basic&quot;</span> <span class="hljs-attr">:class</span>=<span class="hljs-string">&quot;mood&quot;</span>&gt;</span>&#123;&#123;name&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br> <span class="hljs-comment">&lt;!-- 绑定class样式--数组写法，适用于：要绑定的样式个数不确定、名字也不确定 --&gt;</span><br> <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;basic&quot;</span> <span class="hljs-attr">:class</span>=<span class="hljs-string">&quot;classArr&quot;</span>&gt;</span>&#123;&#123;name&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br> <span class="hljs-comment">&lt;!-- 绑定class样式--对象写法，适用于：要绑定的样式个数确定、名字也确定，但要动态决定用不用 --&gt;</span><br> <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;basic&quot;</span> <span class="hljs-attr">:class</span>=<span class="hljs-string">&quot;classObj&quot;</span>&gt;</span>&#123;&#123;name&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br><br>data:&#123;<br>     name: &#x27;尚硅谷&#x27;,<br>     mood: &#x27;normal&#x27;,<br>     classArr: [&#x27;atguigu1&#x27;, &#x27;atguigu2&#x27;, &#x27;atguigu3&#x27;],<br>     classObj: &#123;<br>         atguigu1: false,<br>         atguigu2: false,<br>     &#125;, <br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="1-5-2-绑定style样式"><a href="#1-5-2-绑定style样式" class="headerlink" title="1.5.2 绑定style样式"></a>1.5.2 绑定style样式</h3><blockquote>
<p>写法：<code>:style=&quot;&#123;fontSize: xxx&#125;&quot; </code>中xxx是动态值</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-comment">&lt;!--绑定style样式--对象写法--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;basic&quot;</span> <span class="hljs-attr">:sty1e</span>=<span class="hljs-string">&quot;sty1e0bj&quot;</span>&gt;</span>&#123;&#123;name&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><br>data:&#123;<br>    styleObj: &#123;<br>        fontSize:40px,<br>        color:red<br>    &#125;,<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="1-5-3-条件渲染"><a href="#1-5-3-条件渲染" class="headerlink" title="1.5.3 条件渲染"></a>1.5.3 条件渲染</h3><h4 id="v-if"><a href="#v-if" class="headerlink" title="v-if"></a>v-if</h4><ul>
<li><p>写法跟if-else语法类似</p>
<p>&#x3D;&#x3D;v-if&#x3D;“表达式”&#x3D;&#x3D;</p>
<p>&#x3D;&#x3D;v-else-if&#x3D;“表达式”&#x3D;&#x3D;</p>
<p>&#x3D;&#x3D;v-else&#x3D;&#x3D;</p>
</li>
</ul>
<blockquote>
<p>适用于：切换频率较低的场暴，因为不展示的<strong>DOM</strong>元素直接被移除</p>
<p>注意:<code>v-if </code>可以和<code>v-else-if</code> <code>v-else</code>起使用，但要求<strong>结构不能被打断</strong></p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-comment">&lt;!-- 使用v-if做条件渲染 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">v-if</span>=<span class="hljs-string">&quot;n === 1&quot;</span>&gt;</span>Angular<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">v-else-if</span>=<span class="hljs-string">&quot;n === 2&quot;</span>&gt;</span>React<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">v-else-if</span>=<span class="hljs-string">&quot;n === 3&quot;</span>&gt;</span>Vue<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">v-else</span>&gt;</span>哈哈<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h4 id="v-show"><a href="#v-show" class="headerlink" title="v-show"></a>v-show</h4><ul>
<li>写法：<code>v-show=&quot;表达式&quot;</code></li>
</ul>
<blockquote>
<p>适用于：切换频率较高的场暴</p>
<p>特点：不展示的<code>DOM</code>元素被移除，仅仅是使用样式隐藏掉<code>display：none</code></p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;!-- 使用v-show做条件渲染 --&gt;<br>&lt;h2 v-show=&quot;false&quot;&gt;欢迎来到&#123;&#123;name&#125;&#125;&lt;/h2&gt;<br>&lt;h2 v-show=&quot;1 === 1&quot;&gt;欢迎来到&#123;&#123;name&#125;&#125;&lt;/h2&gt;<br></code></pre></td></tr></table></figure>

<h2 id="1-6-列表渲染和数据监视"><a href="#1-6-列表渲染和数据监视" class="headerlink" title="1.6 列表渲染和数据监视"></a>1.6 列表渲染和数据监视</h2><h3 id="1-6-1-列表渲染"><a href="#1-6-1-列表渲染" class="headerlink" title="1.6.1 列表渲染"></a>1.6.1 列表渲染</h3><h4 id="1-6-1-1-基本列表"><a href="#1-6-1-1-基本列表" class="headerlink" title="1.6.1.1 基本列表"></a>1.6.1.1 基本列表</h4><p><code>v-for</code> 指令用于展示列表数据</p>
<ul>
<li>语法： <code>&lt;li v-for=&quot;(item, index) of items&quot; :key=&quot;index&quot;&gt;</code>，这里 key 可以是 index ，更好的是遍历对象的唯一标识 可遍历：数组、对象、字符串（用的少）、指定次数（用的少）</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs html"> <span class="hljs-comment">&lt;!-- 遍历数组 --&gt;</span><br> <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>人员列表（遍历数组）<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span><br> <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;(p,index) of persons&quot;</span> <span class="hljs-attr">:key</span>=<span class="hljs-string">&quot;index&quot;</span>&gt;</span>&#123;&#123; p.name &#125;&#125;-&#123;&#123; p.age &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br> <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span><br> <span class="hljs-comment">&lt;!-- 遍历对象 --&gt;</span><br> <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>汽车信息（遍历对象）<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span><br> <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;(value,k) of car&quot;</span> <span class="hljs-attr">:key</span>=<span class="hljs-string">&quot;k&quot;</span>&gt;</span>&#123;&#123; k &#125;&#125;-&#123;&#123; value &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br> <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span><br> <span class="hljs-comment">&lt;!-- 遍历字符串 --&gt;</span><br> <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>测试遍历字符串（用得少）<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span><br> <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;(char,index) of str&quot;</span> <span class="hljs-attr">:key</span>=<span class="hljs-string">&quot;index&quot;</span>&gt;</span>&#123;&#123; char &#125;&#125;-&#123;&#123; index &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br> <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span><br> <span class="hljs-comment">&lt;!-- 遍历指定次数 --&gt;</span><br> <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>测试遍历指定次数（用得少）<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span><br> <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;(number,index) of 5&quot;</span> <span class="hljs-attr">:key</span>=<span class="hljs-string">&quot;index&quot;</span>&gt;</span>&#123;&#123; index &#125;&#125;-&#123;&#123; number &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br> <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><br><br> data: &#123;<br>     persons: [<br>         &#123; id: &#x27;001&#x27;, name: &#x27;张三&#x27;, age: 18 &#125;,<br>         &#123; id: &#x27;002&#x27;, name: &#x27;李四&#x27;, age: 19 &#125;,<br>         &#123; id: &#x27;003&#x27;, name: &#x27;王五&#x27;, age: 20 &#125;<br>     ],<br>     car: &#123;<br>         name: &#x27;奥迪A8&#x27;,<br>         price: &#x27;70万&#x27;,<br>         color: &#x27;黑色&#x27;<br>     &#125;,<br>     str: &#x27;hello&#x27;<br> &#125;<br><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/blog/img/loading.gif" lazyload data-original="https://markdown-1307246189.cos.ap-chongqing.myqcloud.com/markdown/image-20221013214237981.png" alt="image-20221013214237981"></p>
<h4 id="1-6-1-2-列表过滤"><a href="#1-6-1-2-列表过滤" class="headerlink" title="1.6.1.2 列表过滤"></a>1.6.1.2 列表过滤</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;root&quot;</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>人员列表<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">&quot;请输入名字&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;keyWord&quot;</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span><br>         <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;(p,index) of filPersons&quot;</span> <span class="hljs-attr">:key</span>=<span class="hljs-string">&quot;p.id&quot;</span>&gt;</span><br>             &#123;&#123; p.name &#125;&#125;-&#123;&#123; p.age &#125;&#125;-&#123;&#123; p.sex &#125;&#125;<br>         <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>     <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><br>data: &#123;<br>    keyWord: &#x27;&#x27;,<br>    persons: [<br>        &#123; id: &#x27;001&#x27;, name: &#x27;马冬梅&#x27;, age: 19, sex: &#x27;女&#x27; &#125;,<br>        &#123; id: &#x27;002&#x27;, name: &#x27;周冬雨&#x27;, age: 20, sex: &#x27;女&#x27; &#125;,<br>        &#123; id: &#x27;003&#x27;, name: &#x27;周杰伦&#x27;, age: 21, sex: &#x27;男&#x27; &#125;,<br>        &#123; id: &#x27;004&#x27;, name: &#x27;温兆伦&#x27;, age: 22, sex: &#x27;男&#x27; &#125;<br>    ],<br>    filPersons: []<br>    &#125;,<br>    watch: &#123;<br>        keyWord: &#123;<br>        immediate: true,<br>        handler(val) &#123;<br>        this.filPersons = this.persons.filter((p) =&gt; &#123;<br>        return p.name.indexOf(val) !== -1<br>        &#125;)<br>        &#125;<br>    &#125;<br> &#125;<br></code></pre></td></tr></table></figure>

<h3 id="1-6-2-数据监视"><a href="#1-6-2-数据监视" class="headerlink" title="1.6.2 数据监视"></a>1.6.2 数据监视</h3><p>更新时的一个问题</p>
<p><code>this.persons[0] = &#123;id:&#39;001&#39;,name:&#39;马老师,age:50,sex:&#39;男&quot;&#125;</code>更改<code>data</code>数据，<code>Vue</code>不监听，模板不改变。</p>
<p>原理</p>
<ol>
<li><p><code>vue</code>会监视<code>data</code>中所有层次的数据</p>
</li>
<li><p>如何监测<code>对象</code>中的数据?<br>通过<code>setter</code>实现监视，且要在<code>new Vue()</code>时就传入要监测的数据</p>
<ul>
<li>对象<code>创建后</code>追加的属性，Vue 默认不做响应式处理</li>
<li>如需给后添加的属性做响应式，请使用如下API<ul>
<li>&#x3D;&#x3D;Vue. set(target, propertyName&#x2F; index, value)&#x3D;&#x3D;，即<code>Vue.set(对象,&#39;属性&#39;,&#39;属性值&#39;)</code></li>
<li>&#x3D;&#x3D;vm. $set(target , propertyName&#x2F; index,value )&#x3D;&#x3D;</li>
</ul>
</li>
</ul>
</li>
<li><p>如何监测<code>数组</code>中的数据?</p>
<p>通过包裹数组更新元素的方法实现，本质就是做了两件事</p>
<p>a. 调用原生对应的方法对数组进行更新，是对整个数组做改动，因为vue并不为数组中某一个元素单独生成getter，setter方法</p>
<p>b. 重新解析模板，进而更新页面</p>
</li>
<li><p>在<code>Vue</code>修改数组中的某个元素一定要用如下方法</p>
<ul>
<li>&#x3D;&#x3D;push()&#x3D;&#x3D;：该方法是在数组最后添加数据，并返回数组长度</li>
<li>&#x3D;&#x3D;pop()&#x3D;&#x3D;：删除最后一个元素，并返回删除的元素值</li>
<li>&#x3D;&#x3D;shift()&#x3D;&#x3D;：删除数组的第一个元素，返回第一个删除的值</li>
<li>&#x3D;&#x3D;unshift()&#x3D;&#x3D;：向数组开头添加元素，返回数组长度</li>
<li>&#x3D;&#x3D;reverse()&#x3D;&#x3D;：翻转数组</li>
<li>&#x3D;&#x3D;sort(function(a,b){return a-b})&#x3D;&#x3D;：数组排序升序&#x2F;b-a降序</li>
<li>&#x3D;&#x3D;splice(index,howmany,arr1,arr2…)&#x3D;&#x3D; ：用于添加或删除数组中的元素。从【index】位置开始删除howmany个元素，并将arr1、arr2…数据从index位置依次插入。howmany为0时，则不删除元素。</li>
</ul>
<blockquote>
<p>特别注意:<code> Vue.set()</code> 和<code>vm.$set()</code>.不能给<code>vm</code>或<code>vm</code>的根数据对象( <code>data</code>等)添加属性</p>
</blockquote>
</li>
</ol>
<h2 id="1-7-收集表单数据"><a href="#1-7-收集表单数据" class="headerlink" title="1.7 收集表单数据"></a>1.7 收集表单数据</h2><p>收集表单数据 </p>
<ul>
<li><p>若 <input type="text">，则<code>v-model</code>收集的是 <code>value</code> 值，用户输入的内容就是<code>value</code>值 </p>
</li>
<li><p>若 <input type="radio"> ，则<code> v-model</code> 收集的是<code>value</code>值，且要给标签配置 <code>value</code> 属性 </p>
</li>
<li><p>若 <input type="checkbox"></p>
<ul>
<li>没有配置<code>value</code>属性，那么收集的是<code>checked</code>属性（勾选 or 未勾选，是布尔值）</li>
<li>配置了 value 属性 </li>
<li><code>v-model </code>的初始值是<code>非数组</code>，那么收集的就是<code>checked</code>（勾选 or 未勾选，是布尔值）</li>
<li><code>v-model</code>的初始值是<code>数组</code>，那么收集的就是<code>value</code>组成的数组</li>
</ul>
</li>
</ul>
<p> v-model 的三个修饰符</p>
<ul>
<li><code>lazy </code>失去焦点后再收集数据</li>
<li><code>number </code>输入字符串转为有效的数字 </li>
<li><code>trim </code>输入首尾空格过滤</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;root&quot;</span>&gt;</span><br> <span class="hljs-tag">&lt;<span class="hljs-name">form</span> @<span class="hljs-attr">submit.prevent</span>=<span class="hljs-string">&quot;demo&quot;</span>&gt;</span><br>     账号：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">v-model.trim</span>=<span class="hljs-string">&quot;userInfo.account&quot;</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span><br>     密码：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;password&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;userInfo.password&quot;</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span><br>     年龄：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;number&quot;</span> <span class="hljs-attr">v-model.number</span>=<span class="hljs-string">&quot;userInfo.age&quot;</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span><br>     性别：<br>     男<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;radio&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;sex&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;userInfo.sex&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;male&quot;</span>&gt;</span><br>     女<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;radio&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;sex&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;userInfo.sex&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;female&quot;</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span><br>     爱好：<br>     学习<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;userInfo.hobby&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;study&quot;</span>&gt;</span><br>     打游戏<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;userInfo.hobby&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;game&quot;</span>&gt;</span><br>     吃饭<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;userInfo.hobby&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;eat&quot;</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span><br>     所属校区<br>     <span class="hljs-tag">&lt;<span class="hljs-name">select</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;userInfo.city&quot;</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;&quot;</span>&gt;</span>请选择校区<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;beijing&quot;</span>&gt;</span>北京<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;shanghai&quot;</span>&gt;</span>上海<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;shenzhen&quot;</span>&gt;</span>深圳<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;wuhan&quot;</span>&gt;</span>成都<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span><br>     <span class="hljs-tag">&lt;/<span class="hljs-name">select</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>     其他信息：<br>     <span class="hljs-tag">&lt;<span class="hljs-name">textarea</span> <span class="hljs-attr">v-model.lazy</span>=<span class="hljs-string">&quot;userInfo.other&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">textarea</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;userInfo.agree&quot;</span>&gt;</span>阅读并接受<br>     <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;https://www.yuque.com/cessstudy&quot;</span>&gt;</span>《用户协议》<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>提交<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br> <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"> <span class="hljs-title class_">Vue</span>.<span class="hljs-property">config</span>.<span class="hljs-property">productionTip</span> = <span class="hljs-literal">false</span></span><br><span class="language-javascript"> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Vue</span>(&#123;</span><br><span class="language-javascript">     <span class="hljs-attr">el</span>: <span class="hljs-string">&#x27;#root&#x27;</span>,</span><br><span class="language-javascript">     <span class="hljs-attr">data</span>: &#123;</span><br><span class="language-javascript">         <span class="hljs-attr">userInfo</span>: &#123;</span><br><span class="language-javascript">             <span class="hljs-attr">account</span>: <span class="hljs-string">&#x27;&#x27;</span>,</span><br><span class="language-javascript">             <span class="hljs-attr">password</span>: <span class="hljs-string">&#x27;&#x27;</span>,</span><br><span class="language-javascript">             <span class="hljs-attr">age</span>: <span class="hljs-number">18</span>,</span><br><span class="language-javascript">             <span class="hljs-attr">sex</span>: <span class="hljs-string">&#x27;female&#x27;</span>,</span><br><span class="language-javascript">             <span class="hljs-attr">hobby</span>: [],</span><br><span class="language-javascript">             <span class="hljs-attr">city</span>: <span class="hljs-string">&#x27;beijing&#x27;</span>,</span><br><span class="language-javascript">             <span class="hljs-attr">other</span>: <span class="hljs-string">&#x27;&#x27;</span>,</span><br><span class="language-javascript">             <span class="hljs-attr">agree</span>: <span class="hljs-string">&#x27;&#x27;</span></span><br><span class="language-javascript">         &#125;</span><br><span class="language-javascript">     &#125;,</span><br><span class="language-javascript">     <span class="hljs-attr">methods</span>: &#123;</span><br><span class="language-javascript">         <span class="hljs-title function_">demo</span>(<span class="hljs-params"></span>) &#123;</span><br><span class="language-javascript">         <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">userInfo</span>))</span><br><span class="language-javascript">         &#125;</span><br><span class="language-javascript">     &#125;</span><br><span class="language-javascript"> &#125;)</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="1-8-内置指令和自定义指令"><a href="#1-8-内置指令和自定义指令" class="headerlink" title="1.8 内置指令和自定义指令"></a>1.8 内置指令和自定义指令</h2><h3 id="1-8-1-内置指令"><a href="#1-8-1-内置指令" class="headerlink" title="1.8.1 内置指令"></a>1.8.1 内置指令</h3><p>前面提到过的指令：</p>
<ul>
<li><code>v- bind</code>       单向绑定解析表达式，可简写为<code>:</code></li>
<li><code>v-model</code>       双向数据绑定</li>
<li><code>v-for </code>           遍历数组&#x2F;对象&#x2F;字符串.</li>
<li><code>v-on   </code>             绑定事件监听，可简写为@</li>
<li><code>v-show</code>         条件渲染(动态控制节点是否展示)</li>
<li><code>v- if </code>           条件渲染(动态控制节点是否存存在)</li>
<li><code>v-else-if </code>   条件渲染 (动态控制节点是否存存在)</li>
<li><code>v-else </code>         条件渲染(动态控制节点是否存存在)</li>
</ul>
<h4 id="1-8-1-1-v-text"><a href="#1-8-1-1-v-text" class="headerlink" title="1.8.1.1 v-text"></a>1.8.1.1 v-text</h4><p>作用：向其所在的节点中渲染<code>文本内容</code>(不解析标签，全部当成文本)</p>
<p>与插值语法的区别：<code>v-text</code>会替换掉节点中的内容，<code>&#123;&#123;xxx&#125;&#125;</code> 则不会，更灵活</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;root&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>你好，&#123;&#123;name&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">v-text</span>=<span class="hljs-string">&quot;name&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><br>new Vue(&#123;<br>    el:&#x27;#root&#x27;,<br>    data:&#123;<br>        name:&#x27;cess&quot; ，<br>    &#125;<br>&#125;)<br></code></pre></td></tr></table></figure>

<h4 id="1-8-1-2-v-html"><a href="#1-8-1-2-v-html" class="headerlink" title="1.8.1.2 v-html"></a>1.8.1.2 v-html</h4><p>作用：向指定节点中渲染包含<code>html</code>结构的内容（解析标签）</p>
<p>与插值语法的区别:</p>
<ul>
<li><code>v-html</code> 会替换掉节点中所有的内容， <code>&#123;&#123;xxx&#125;&#125;</code> 则不会</li>
<li><code>v-html</code>可以识别<code>html</code>结构</li>
</ul>
<blockquote>
<p>严重注意v-html有安全性问题! !</p>
</blockquote>
<ul>
<li>在网站上动态渲染任意<code>html</code>是非常危险的，容易导致XSS攻击</li>
<li>一定要在可信的内容上使用<code>v-html</code> ，永远不要用在用户提交的内容上! ! !</li>
</ul>
<h4 id="1-8-1-3-v-clock"><a href="#1-8-1-3-v-clock" class="headerlink" title="1.8.1.3 v-clock"></a>1.8.1.3 v-clock</h4><ul>
<li>本质是一个特殊属性，<code>Vue</code>实例创建完毕并接管容器后，会删掉<code>v-cloak</code>属性</li>
<li>使用<code>css</code>配合<code>v-cloak</code> 可以解决网速慢时页面展示出<code>&#123;&#123;xxx&#125;&#125;</code>的问题</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>v-cloak指令<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">sty1e</span>&gt;</span><br>    //选中所有含有v-cloak属性的标签<br>    [v-cloak] &#123;<br>        display:none;<br>    &#125;<br><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;root&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">v-cloak</span>&gt;</span>&#123;&#123; name &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>//延迟5秒收到vue.js<br><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;http://localhost: 8080/resource/5s/vue.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>= <span class="hljs-string">&quot;text/javascript&quot;</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript">    <span class="hljs-variable language_">console</span>.1og(<span class="hljs-number">1</span>)</span><br><span class="language-javascript">    <span class="hljs-title class_">Vue</span>.<span class="hljs-property">config</span>.<span class="hljs-property">productionTip</span> = <span class="hljs-literal">false</span></span><br><span class="language-javascript">    <span class="hljs-keyword">new</span> <span class="hljs-title class_">Vue</span>(&#123;</span><br><span class="language-javascript">        <span class="hljs-attr">el</span>:<span class="hljs-string">&#x27;#root&#x27;</span>,</span><br><span class="language-javascript">        <span class="hljs-attr">data</span>:&#123;<span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;cess&#x27;</span>&#125;</span><br><span class="language-javascript">    &#125;)</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h4 id="1-8-1-4-v-once"><a href="#1-8-1-4-v-once" class="headerlink" title="1.8.1.4 v-once"></a>1.8.1.4 v-once</h4><ul>
<li><code>v-once</code>所在节点在初次动态渲染后，就视为静态内容了</li>
<li>以后数据的改变不会引起起<code>v-once</code>所在结构的更新，可以用于优化性能</li>
</ul>
<h4 id="1-8-1-5-v-pre"><a href="#1-8-1-5-v-pre" class="headerlink" title="1.8.1.5 v-pre"></a>1.8.1.5 v-pre</h4><ul>
<li>跳过v-pre所在节点的编译过程</li>
<li>可利用它跳过：没有使用指令语法、没有使用插值语法的节点，会加快编译</li>
</ul>
<h3 id="1-8-2-自定义指令"><a href="#1-8-2-自定义指令" class="headerlink" title="1.8.2 自定义指令"></a>1.8.2 自定义指令</h3><p><code>directives </code>，与methods同级</p>
<blockquote>
<p>指令定义时不加v-，但使用时要加v-</p>
<p>指令名如果是多个单词，要使用kebab -case命名方式，不要用camelCase命名</p>
</blockquote>
<h4 id="1-8-2-1-函数式"><a href="#1-8-2-1-函数式" class="headerlink" title="1.8.2.1 函数式"></a>1.8.2.1 函数式</h4><p>需求1：定义一个v-big指令， 和v-text功能类似，但会把绑定的数值放大10倍。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>放大10倍后的n值是: <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">v-big</span>=<span class="hljs-string">&quot;n&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;n++&quot;</span>&gt;</span>点我n+1<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br><br>directives: &#123;<br>    // big函数何时会被调用?<br>    // 1.指令与元素成功绑定时(一上来) 2.指令所在的模板被重新解析时<br>    big(element, binding) &#123;<br>        element.innerText = binding.value * 10<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="1-8-2-2-对象式"><a href="#1-8-2-2-对象式" class="headerlink" title="1.8.2.2 对象式"></a>1.8.2.2 对象式</h4><p>需求2：定义一个v- Fbind指令，和v bind功能拳似，但可以让:其所绑定的input元素默认获取焦点。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text” v-fbind:value=&quot;</span><span class="hljs-attr">n</span>&quot;&gt;</span> <br>                                    <br>directives: &#123;<br>    fbind:&#123;<br>        //指令与元素成功绑定时(上来)<br>        bind(element,binding)&#123;<br>           element.innerText = binding.value<br>        &#125;,<br>        //指令所在元素被插入页面时                          <br>        inserted(element,binding)&#123;<br>           element.focus()                     <br>        &#125;,<br>        //指令所在的模板被重新解析时                           <br>        update(element,binding)&#123;<br>           element.innerText = binding.value                        <br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h1 id="二、Vue-Cli"><a href="#二、Vue-Cli" class="headerlink" title="二、Vue-Cli"></a>二、Vue-Cli</h1><h2 id="2-1-初始化脚手架"><a href="#2-1-初始化脚手架" class="headerlink" title="2.1 初始化脚手架"></a>2.1 初始化脚手架</h2><p><img src="" srcset="/blog/img/loading.gif" lazyload data-original="https://markdown-1307246189.cos.ap-chongqing.myqcloud.com/markdown/image-20221016130824024.png" alt="image-20221016130824024"></p>
<h2 id="2-2-ref-props-mixin-plugin-scoped"><a href="#2-2-ref-props-mixin-plugin-scoped" class="headerlink" title="2.2 ref props mixin plugin scoped"></a>2.2 ref props mixin plugin scoped</h2><h3 id="2-2-1-ref属性"><a href="#2-2-1-ref属性" class="headerlink" title="2.2.1 ref属性"></a>2.2.1 ref属性</h3><p><code>ref</code>被用来给元素或子组件注册引|用信息(id的替代者)</p>
<ul>
<li>应用在html 标签上获取的是真实DOM元素，应用在组件标签上获取的是组件实例对象vc</li>
<li>使用方式<ul>
<li>打标识：<code>&lt;h1 ref=&quot;xx&quot;&gt;&lt;/h1&gt;</code>或<code>&lt;School ref=&quot;xxx&quot; &gt;&lt;/School&gt;</code></li>
<li>获取：<code> this.$refs.xxx</code></li>
</ul>
</li>
</ul>
<h3 id="2-2-2-props配置项"><a href="#2-2-2-props配置项" class="headerlink" title="2.2.2 props配置项"></a>2.2.2 props配置项</h3><p><code>props</code>让组件接收外部传过来的数据，更适用于<code>父传子</code>。与methods同级</p>
<ul>
<li><p>传递数据<code>&lt;Demo name=&quot;xx” :age=&quot;18&quot;/&gt;</code> 这里age前加<code>:</code>,通过v-bind使得里面的18是数字</p>
</li>
<li><p>接收数据</p>
<p>第一种方式(只接收)        <code>props:[&#39;name&#39;,&#39; age&#39;]</code></p>
<p>第二种方式(限制类型)    <code> props:&#123;name:String,age:Number&#125;</code></p>
<p>第三种方式(限制类型、限制必要性、指定默认值)</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-attr">props</span>: &#123;<br>    <span class="hljs-attr">name</span>: &#123;<br>        <span class="hljs-attr">type</span>: <span class="hljs-title class_">String</span>,<span class="hljs-comment">//类型</span><br>        <span class="hljs-attr">required</span>: <span class="hljs-literal">true</span>,<span class="hljs-comment">// 必要性</span><br>        <span class="hljs-attr">default</span>: <span class="hljs-string">&#x27;cess&#x27;</span><span class="hljs-comment">//默认值</span><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure></li>
</ul>
<blockquote>
<p>tip: props是<code>只读</code>的，Vue底层会监测你对props 的修改，如果进行了修改，就会发出警告，若业务需求确实需要修改，那么请复制props 的内容到data中，然后去修改data中的数据。</p>
</blockquote>
<p>父<code>src/App.vue</code></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">Student</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;李四” sex=&quot;</span><span class="hljs-attr">女</span>”<span class="hljs-attr">:age</span>=<span class="hljs-string">&quot;18&quot;</span>/&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">Student</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;王五” sex=&quot;</span><span class="hljs-attr">男</span>” <span class="hljs-attr">:age</span>=<span class="hljs-string">&quot;18&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><br>    import Student from &#x27; ./ components/Student’<br>    export default &#123;<br>        name:&#x27;App&#x27;,<br>        components:&#123; Student &#125;<br>    &#125;<br><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>子<code>src/components/Student.vue</code></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>&#123;&#123; msg &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>学生姓名: &#123;&#123; name &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>学生性别: &#123;&#123; sex &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>学生年龄: &#123;&#123; myAge + 1 &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;updateAge&quot;</span>&gt;</span>尝 试修改收到的年龄<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br>//接收的同时对数据:进行类型限制+默认值的指定+必要性的限制<br>props:&#123;<br>    name:&#123;<br>        type: string，//name的类型 是字符串<br>        required: true //name是必要的<br>    &#125;,<br>    age:&#123;<br>        type:Number,<br>        default:99//默认值<br>    &#125;,<br>    sex:&#123;<br>        type:String,<br>        required:true<br>    &#125;,<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="2-2-3-mixin混入"><a href="#2-2-3-mixin混入" class="headerlink" title="2.2.3 mixin混入"></a>2.2.3 mixin混入</h3><ul>
<li><p>功能：可以把多个组件共用的配置提取成一个混入对象，与data同级</p>
</li>
<li><p>使用方式</p>
<ul>
<li>定义混入<code>src/mixin.js</code></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> hunhe = &#123;<br>    <span class="hljs-attr">methods</span>:&#123;<br>        <span class="hljs-title function_">showName</span>(<span class="hljs-params"></span>)&#123;<br>            <span class="hljs-title function_">alert</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> )<br>        &#125;<br>    &#125;,<br>    <span class="hljs-title function_">mounted</span>(<span class="hljs-params"></span>)&#123;<br>        <span class="hljs-variable language_">console</span>.1og(<span class="hljs-string">&#x27;你好啊! &#x27;</span>)<br>    &#125;,<br> &#125;<br><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> hunhe2 = &#123;<br>    <span class="hljs-title function_">data</span>(<span class="hljs-params"></span>)&#123;<br>        <span class="hljs-keyword">return</span> &#123;<br>            <span class="hljs-attr">x</span>:<span class="hljs-number">100</span>,<br>            <span class="hljs-attr">y</span>:<span class="hljs-number">200</span><br>        &#125;<br>    &#125;,<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>使用混入</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">h2</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;showName&quot;</span>&gt;</span> 学校名称: &#123;&#123;name&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-comment">//引入一个hunhe</span></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> &#123;hunhe , hunhe2&#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../mixin&#x27;</span></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;School&#x27;</span>,</span><br><span class="language-javascript">    <span class="hljs-attr">mixins</span>: [hunhe, hunhe2] <span class="hljs-comment">//局部混入</span></span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure></li>
</ul>
<h3 id="2-2-4-plugin插件"><a href="#2-2-4-plugin插件" class="headerlink" title="2.2.4 plugin插件"></a>2.2.4 plugin插件</h3><ul>
<li><p>功能：增强Vue</p>
</li>
<li><p>本质：包含install 方法的一个对象，install 的第一个参数是Vue，第二个以后的参数是插件使用者传递的数据</p>
</li>
<li><p>定义插件<code>src/plugin.js</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;<br>    <span class="hljs-title function_">install</span>(<span class="hljs-params">Vue,x,y,z</span>)&#123;<br>        <span class="hljs-variable language_">console</span>.1og(x,Y,z)<br>        <span class="hljs-comment">//全局过滤器</span><br>        <span class="hljs-title class_">Vue</span>. <span class="hljs-title function_">filter</span>(<span class="hljs-string">&#x27;mySlice&#x27;</span>, <span class="hljs-keyword">function</span>(<span class="hljs-params">value</span>)&#123;<span class="hljs-keyword">return</span> value. <span class="hljs-title function_">slice</span>(<span class="hljs-number">0</span>,<span class="hljs-number">4</span>)&#125;)<br>        <br>        <span class="hljs-comment">//定义全局指令</span><br>        <span class="hljs-title class_">Vue</span>. <span class="hljs-title function_">directive</span>(<span class="hljs-string">&#x27;fbind&#x27;</span>,&#123;<br>            <span class="hljs-comment">//指令与元素成功绑定时(一上来)</span><br>            <span class="hljs-title function_">bind</span>(<span class="hljs-params">element,binding</span>)&#123;element.<span class="hljs-property">value</span> = binding.<span class="hljs-property">value</span>&#125;,<br>            <span class="hljs-comment">//指令所在元素被插入页面时</span><br>            <span class="hljs-title function_">inserted</span>(<span class="hljs-params">element,binding</span>)&#123;element.<span class="hljs-title function_">focus</span>()&#125;,<br>            <span class="hljs-comment">//指令所在的模板被重新解析时</span><br>            <span class="hljs-title function_">update</span>(<span class="hljs-params">element,binding</span>)&#123;element.<span class="hljs-property">value</span> = binding.<span class="hljs-property">value</span>&#125;<br>        &#125;)<br>        <span class="hljs-comment">//定义混入</span><br>        <span class="hljs-title class_">Vue</span>.<span class="hljs-title function_">mixin</span>(&#123;<br>            <span class="hljs-title function_">data</span>(<span class="hljs-params"></span>)&#123;<span class="hljs-keyword">return</span> &#123;<span class="hljs-attr">x</span>:<span class="hljs-number">100</span>,<span class="hljs-attr">y</span>:<span class="hljs-number">200</span>&#125;&#125;,<br>        &#125;)<br>        <span class="hljs-comment">//给Vue原型_上添加一个方法(vm和vc就都能用了)</span><br>         <span class="hljs-title class_">Vue</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">hello</span> = <span class="hljs-function">()=&gt;</span>&#123;<span class="hljs-title function_">alert</span>(<span class="hljs-string">&#x27; 你好啊&#x27;</span>)&#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
</li>
<li><p>使用插件：<code>Vue.use() </code>   <code> src/main.js</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">import</span> plugins <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;/plugins&#x27;</span> <span class="hljs-comment">// 引入插件</span><br><span class="hljs-title class_">Vue</span>.<span class="hljs-property">config</span>.<span class="hljs-property">productionTip</span> = <span class="hljs-literal">false</span><br><span class="hljs-title class_">Vue</span>.<span class="hljs-title function_">use</span>(plugins,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>) <span class="hljs-comment">//应用(使用)插件</span><br><br><span class="hljs-keyword">new</span> <span class="hljs-title class_">Vue</span>(&#123;<br>    <span class="hljs-attr">el</span>:<span class="hljs-string">&quot;#app&quot;</span>,<br>    <span class="hljs-attr">render</span>: <span class="hljs-function"><span class="hljs-params">h</span> =&gt;</span> <span class="hljs-title function_">h</span>(<span class="hljs-title class_">App</span>)<br>&#125;)<br></code></pre></td></tr></table></figure>

<p><code>src/ components/ School. vue</code></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>学校名称: &#123;&#123; name | mySlice &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>学校地址: &#123;&#123; address &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">c1ick</span>=<span class="hljs-string">&quot;test&quot;</span>&gt;</span>点我测试一个he11o方法<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><br>    export default &#123;<br>        name:&#x27;School&#x27;,<br>        data() &#123;<br>            return &#123;<br>                name:&#x27;尚硅谷atguigu&#x27;,<br>                address:&quot;北京&#x27;,<br>            &#125;<br>        &#125;,   <br>        methods: &#123;<br>            test()&#123;<br>                this. hello()<br>            &#125;<br>        &#125;<br>    &#125;，<br><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure></li>
</ul>
<h3 id="2-2-5-scoped样式"><a href="#2-2-5-scoped样式" class="headerlink" title="2.2.5 scoped样式"></a>2.2.5 scoped样式</h3><ul>
<li>作用:让样式在局部生效，防止冲突</li>
<li>写法：<code>&lt;style scoped&gt;</code></li>
</ul>
<h2 id="2-3-本地存储-自定义事件"><a href="#2-3-本地存储-自定义事件" class="headerlink" title="2.3 本地存储 自定义事件"></a>2.3 本地存储 自定义事件</h2><h3 id="2-3-1-本地存储"><a href="#2-3-1-本地存储" class="headerlink" title="2.3.1 本地存储"></a>2.3.1 本地存储</h3><p>存储内容大小一般支持5MB左右(不同浏览器可能还不一样)</p>
<p>浏览器端通过<code>Window. sessionStorage</code>和<code>Window. localStorage</code>属性来实现本地存储机制<br>相关API</p>
<ul>
<li><code>xxxStorage. setItem( &#39;key&#39;, &#39;value&#39; )</code>该访法接受一个键和值作为参数, 会把键值对添加到存储中,<br>如果键名存在，则更新其对应的值</li>
<li><code>xxxStorage. getItem( &#39;key&#39;)</code>该访法接受一个键名作为参数, 返回键名对应的值</li>
<li><code>xxxStorage. removeItem( &#39;key&#39; )</code>该访法接受一个键名作为参数, 并把该键名从存储中删除</li>
<li><code>xxxStorage.clear()</code>该访法会清空存储中的所有数据</li>
</ul>
<blockquote>
<p>SessionStorage 存储的内容会随着浏览器窗口关闭而消失</p>
<p>LocalStorage 存储的内容,需要手动清除才会消失</p>
<p>xxxStorage. getItem(xxx) 如果xxx对应的value获取不到，那么getItem() 的返回值是null</p>
<p>JSON. parse(null)的结果依然是null</p>
</blockquote>
<h3 id="2-3-2-自定义事件"><a href="#2-3-2-自定义事件" class="headerlink" title="2.3.2 自定义事件"></a>2.3.2 自定义事件</h3><ul>
<li><p>一种<code>组件间</code>通信的方式，适用于<code>子传父</code></p>
</li>
<li><p>使用场景：子组件想给父组件传数据，那么就要在父组件中给子组件绑定自定义事件(事件的回调在父组件中)</p>
</li>
<li><p>绑定自定义事件</p>
<ul>
<li><p>第一种方式，在父组件中<code>&lt;Demo @事件名=&quot;方法&quot;/&gt;</code>或<code>&lt;Demo v-on:事件名=&quot;方法&quot;/&gt;</code></p>
<p>在<code>src/App.vue</code>父组件中</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">Student</span> @<span class="hljs-attr">atguigu</span>=<span class="hljs-string">&quot;demo&quot;</span>/&gt;</span><br><br>methods: &#123;<br>    demo(name)&#123;<br>        console.log(&#x27;demo被调用了!&#x27;,name)<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>在<code>src/components/Student.vue</code>子组件中</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;sendStudentName”&gt; 把学生名给App &lt;/button&gt;</span></span><br><span class="hljs-string"><span class="hljs-tag"></span></span><br><span class="hljs-string"><span class="hljs-tag">methods:&#123;</span></span><br><span class="hljs-string"><span class="hljs-tag">    sendStudentName()&#123;</span></span><br><span class="hljs-string"><span class="hljs-tag">        //触发Student组件实例身上的atguigu事件</span></span><br><span class="hljs-string"><span class="hljs-tag">        this.$emit(&#x27;atguigu&#x27;,this.name)</span></span><br><span class="hljs-string"><span class="hljs-tag">    &#125;</span></span><br><span class="hljs-string"><span class="hljs-tag">&#125;</span></span><br></code></pre></td></tr></table></figure>
</li>
<li><p>第二种方式，在父组件中绑定自定义事件<code>this.$refs.demo.$on(&#39;事件名&#39;,方法)</code></p>
<p>在<code>src/App.vue</code>父组件中</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">Student</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&quot;student&quot;</span>/&gt;</span><br><br>methods: &#123;<br>    demo(name)&#123;<br>        console.log(&#x27;demo被调用了!&#x27;,name)<br>    &#125;<br>&#125;,<br>mounted()&#123;<br>    this.$refs.student.$on(&#x27;atguigu&#x27;,this.demo)<br>&#125;<br></code></pre></td></tr></table></figure>

<p>在<code>src/components/Student.vue</code>子组件中触发自定义事件<code>this.$emit(&#39;事件名&#39;,数据)</code></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;sendStudentName”&gt; 把学生名给App &lt;/button&gt;</span></span><br><span class="hljs-string"><span class="hljs-tag"></span></span><br><span class="hljs-string"><span class="hljs-tag">methods:&#123;</span></span><br><span class="hljs-string"><span class="hljs-tag">    sendStudentName()&#123;</span></span><br><span class="hljs-string"><span class="hljs-tag">        //触发Student组件实例身上的atguigu事件</span></span><br><span class="hljs-string"><span class="hljs-tag">        this.$emit(&#x27;atguigu&#x27;,this.name)</span></span><br><span class="hljs-string"><span class="hljs-tag">    &#125;</span></span><br><span class="hljs-string"><span class="hljs-tag">&#125;</span></span><br></code></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>解绑自定义事件<code>this.$off(&#39;事件名&#39;)</code></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>= <span class="hljs-string">&quot;unbind&quot;</span>&gt;</span> 解绑atguigu事件 <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br><br>mounted()&#123;<br>    unbind()&#123;<br>        this.$off(&#x27;atguigu&#x27;)         //解绑多个自定义事件<br>        this.$off([&#x27;atguigu&#x27;,demo&#x27;]) //解绑多个自定义事件<br>        this.$off()                  //解绑所有自定义事件<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
</li>
<li><p>组件也可以绑定原生<code>DOM</code>事件，需要使用<code>native </code>修饰符<code>@click.native=&quot;show&quot;</code>，上面绑定自定义事件，即使绑定的是原生事件也会被认为是自定义的，需</p>
<p>要加<code>native</code> ，加了后就将此事件给组件的根元素</p>
<p>在<code>src/App.vue</code>组件中</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">Student</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&quot;student&quot;</span>  @<span class="hljs-attr">click.native</span>=<span class="hljs-string">&quot;show&quot;</span>/&gt;</span><br><br>methods:&#123;<br>    show()&#123;<br>        alert(123)<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>


</li>
<li><p>注意：通过<code>this.$refs.xxx.$on( &#39;事件名&#39;,回调函数)</code>绑定自定义事件时，回调函数要么配置在<code>methods</code>中，要么用箭头函数，否则<code>this</code>指向会出问题</p>
<blockquote>
<p>因为默认该回调函数中的this是触发事件的vue实例，写在methods中的this一定是当前实例，后者优先级更高</p>
</blockquote>
</li>
</ul>
<h2 id="2-4-全局事件总线-消息的订阅与发布"><a href="#2-4-全局事件总线-消息的订阅与发布" class="headerlink" title="2.4 全局事件总线 消息的订阅与发布"></a>2.4 全局事件总线 消息的订阅与发布</h2><h3 id="2-4-1-全局事件总线"><a href="#2-4-1-全局事件总线" class="headerlink" title="2.4.1 全局事件总线"></a>2.4.1 全局事件总线</h3><p>一种可以在<code>任意组件间</code>通信的方式，本质上就是一个对象， 它必须满足以下条件</p>
<ul>
<li>所有的组件对象都必须能看见他</li>
<li>这个对象必须能够使用<code>$on</code>  <code>$emit</code>  <code>$off</code> 方法去绑定、触发和解绑事件</li>
</ul>
<p>使用步骤</p>
<ul>
<li>定义全局事件总线</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">//创建周转实例$bus</span><br><span class="hljs-keyword">new</span> <span class="hljs-title class_">Vue</span>(&#123;<br>    <span class="hljs-attr">el</span>:<span class="hljs-string">&#x27;#app&#x27;</span>,<br>    <span class="hljs-attr">render</span>:<span class="hljs-function"><span class="hljs-params">h</span> =&gt;</span> <span class="hljs-title function_">h</span>(<span class="hljs-title class_">App</span>),<br>    <span class="hljs-title function_">beforeCreate</span>(<span class="hljs-params"></span>) &#123;<br>        <span class="hljs-title class_">Vue</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">$bus</span> = <span class="hljs-variable language_">this</span>  <span class="hljs-comment">//安装全局事件总线,$bus就是当前应用的vm</span><br>    &#125;<br>&#125;)<br></code></pre></td></tr></table></figure>

<ul>
<li><p>使用事件总线</p>
<ul>
<li>接收数据：A组件想接收数据，则在A组件中给<code>$bus</code>绑定自定义事件，事件的回调留在A组件自身</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-title function_">mounted</span>(<span class="hljs-params"></span>) &#123; <br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$on(<span class="hljs-string">&#x27;hello&#x27;</span>,<span class="hljs-function">(<span class="hljs-params">data</span>)=&gt;</span>&#123;<br>        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( <span class="hljs-string">&#x27;我是School组件,收到了数据&#x27;</span>,data)<br>    &#125;)<br>&#125;,<br><span class="hljs-title function_">beforeDestroy</span>(<span class="hljs-params"></span>) &#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$off(<span class="hljs-string">&#x27;hello&#x27;</span>)<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>传递数据，另一组件提供数据，<code>this.$bus.$emit(&#39;xxx&#39;, data)</code></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">//另一组件触发</span><br><span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$emit(<span class="hljs-string">&#x27;hello&#x27;</span>,<span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span>)<br></code></pre></td></tr></table></figure></li>
</ul>
<h3 id="2-4-2-消息的订阅与发布"><a href="#2-4-2-消息的订阅与发布" class="headerlink" title="2.4.2 消息的订阅与发布"></a>2.4.2 消息的订阅与发布</h3><p>消息订阅与发布(pubsub) 消息订阅与发布是一种组件间通信的方式，适用于<code>任意组件间</code>通信</p>
<p>使用步骤</p>
<ul>
<li>安装pubsub：<code>npm i pubsub-js</code></li>
<li>引入：<code>import pubsub from &#39;pubsub-js&#39;</code></li>
<li>接收数据：A组件想接收数据，则在A组件中订阅消息，订阅的回调留在A组件自身</li>
<li>提供数据: pubsub. publish( ‘xxx’, data)</li>
<li>最好在beforeDestroy 钩子中，使用pubsub . unsubscribe(pid)取消订阅</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs js">&lt;!--发布方和接收方都需要引入--&gt;<br><span class="hljs-keyword">import</span> pubsub <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;pubsub-js&#x27;</span><br><br>&lt;!--接收方想接收数据，则订阅消息，且订阅的回调留在本身--&gt;<br><span class="hljs-title function_">mounted</span>(<span class="hljs-params"></span>)&#123; <br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">pubId</span> = pubsub.<span class="hljs-title function_">subscribe</span>(<span class="hljs-string">&#x27;hello&#x27;</span>,<span class="hljs-function">(<span class="hljs-params">masgName,data</span>)=&gt;</span>&#123;<br>        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;有人发布了hello消息,hello消息的回调执行了&#x27;</span>,mesgName,data)<br>    &#125;)<br>&#125;,<br>    <br><span class="hljs-title function_">beforeDestroy</span>(<span class="hljs-params"></span>) &#123;<br>    pubsub.<span class="hljs-title function_">unsubscribe</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">pubId</span>)<br>&#125;<br><br><br>&lt;!--传数据的一端发布消息--&gt;<br>pubsub.<span class="hljs-title function_">publish</span>(<span class="hljs-string">&#x27;hello&#x27;</span>,<span class="hljs-number">666</span>)<br><br><span class="hljs-attr">methods</span>: &#123;<br>    <span class="hljs-title function_">send</span>(<span class="hljs-params"></span>)&#123;<br>        pubsub.<span class="hljs-title function_">publish</span>(<span class="hljs-string">&#x27;hello&#x27;</span>,<span class="hljs-number">666</span>) <span class="hljs-comment">//发布消息</span><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="2-5-nextTick-过渡与动画"><a href="#2-5-nextTick-过渡与动画" class="headerlink" title="2.5 $nextTick 过渡与动画"></a>2.5 $nextTick 过渡与动画</h2><h3 id="2-5-1-nextTic"><a href="#2-5-1-nextTic" class="headerlink" title="2.5.1 $nextTic"></a>2.5.1 $nextTic</h3><p><strong>这是一个生命周期钩子</strong></p>
<p><code>this.$nextTick(回调函数)</code>在下一次<code>DOM</code>更新结束后执行其指定的回调</p>
<p>什么时候用：当改变数据后，要基于更新后的新<code>DOM</code>进行某些操作时，要在<code>nextTick</code>所指定的回调函数中执行</p>
<h3 id="2-5-2-过渡与动画"><a href="#2-5-2-过渡与动画" class="headerlink" title="2.5.2 过渡与动画"></a>2.5.2 过渡与动画</h3><p><code>Vue</code>封装的过度与动画：在插入、更新或移除<code>DOM</code>元素时，在合适的时候给元素添加样式类名</p>
<ul>
<li>准备好样式，按vue规定的格式来<ul>
<li>元素进入的样式<ul>
<li><code>v-enter</code>                     进入的终点</li>
<li><code>v-enter-active</code>       进入过程中</li>
<li><code>v-enter-to</code>               进入的终点</li>
</ul>
</li>
<li>元素离开的样式<ul>
<li><code>v-leave</code>                     离开的终点</li>
<li><code>v-leave-active</code>       离开过程中</li>
<li><code>v-leave-to</code>               离开的终点</li>
</ul>
</li>
</ul>
</li>
<li>使用<code>&lt;transition&gt;</code> 包裹要过度的元素。可以配置<code>name</code>属性，但配置以后需要将上面样式名默认的<code>v</code>换为<code>name</code>中的属性值</li>
<li>要页面一开始就显示动画，需要添加<code>appear</code></li>
<li>若有多个元素需要过度,则需要使用<code>&lt;transition-group&gt;</code>，且每个元素都要指定key 值</li>
</ul>
<p>原生写法：</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs css">&lt;<span class="hljs-selector-tag">h1</span> v-show=&quot;isShow&quot; class=&quot;come&quot;&gt;你好啊! &lt;/<span class="hljs-selector-tag">h1</span>&gt;<br><br>&lt;style&gt;<br>    <span class="hljs-selector-class">.come</span>&#123;<br>        <span class="hljs-attribute">animation</span>: hello <span class="hljs-number">0.5s</span> linear;<br>    &#125;<br>    <span class="hljs-selector-class">.go</span>&#123;<br>        <span class="hljs-attribute">animation</span>: hello <span class="hljs-number">0.5s</span> linear reverse;//reverse反转<br>    &#125;<br><br>    <span class="hljs-keyword">@keyframes</span> hello &#123;<br>        <span class="hljs-selector-tag">from</span>&#123;<br>            <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translateX</span>(-<span class="hljs-number">100%</span>);<br>        &#125;<br>        <span class="hljs-selector-tag">to</span>&#123;<br>            <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translateX</span>(<span class="hljs-number">0px</span>);<br>        &#125;<br>    &#125;<br>&lt;/style&gt;<br></code></pre></td></tr></table></figure>

<p>Vue封装写法：</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs css">&lt;<span class="hljs-attribute">transition</span>&gt;<br>    &lt;<span class="hljs-selector-tag">h1</span> v-show=&quot;isShow&quot;&gt;你好啊!&lt;/<span class="hljs-selector-tag">h1</span>&gt;<br>&lt;/<span class="hljs-attribute">transition</span>&gt;<br><br>&lt;style&gt;<br>    <span class="hljs-selector-class">.v-enter-active</span>&#123;<br>        <span class="hljs-attribute">animation</span>: hello <span class="hljs-number">0.5s</span> linear;<br>    &#125;<br>    <span class="hljs-selector-class">.v-leave-active</span>&#123;<br>        <span class="hljs-attribute">animation</span>: hello <span class="hljs-number">0.5s</span> linear reverse;//reverse反转<br>    &#125;<br><br>    <span class="hljs-keyword">@keyframes</span> hello &#123;<br>        <span class="hljs-selector-tag">from</span>&#123;<br>            <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translateX</span>(-<span class="hljs-number">100%</span>);<br>        &#125;<br>        <span class="hljs-selector-tag">to</span>&#123;<br>            <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translateX</span>(<span class="hljs-number">0px</span>);<br>        &#125;<br>    &#125;<br>&lt;/style&gt;<br><br>//若有多个元素需要过渡<br>&lt;<span class="hljs-attribute">transition</span>-group name=&quot;hello&quot; appear&gt;<br>    &lt;<span class="hljs-selector-tag">h1</span> v-show=&quot;!isShow&quot; key=&quot;<span class="hljs-number">1</span>&quot;&gt;你好啊! &lt;/<span class="hljs-selector-tag">h1</span>&gt;<br>    &lt;<span class="hljs-selector-tag">h1</span> v-show=&quot;isShow&quot;  key-&quot;<span class="hljs-number">2</span>&quot;&gt;尚硅谷! &lt;/<span class="hljs-selector-tag">h1</span>&gt;<br>&lt;/<span class="hljs-attribute">transition</span>-group&gt;<br></code></pre></td></tr></table></figure>

<h3 id="2-5-3-集成第三方动画库Animate-css"><a href="#2-5-3-集成第三方动画库Animate-css" class="headerlink" title="2.5.3 集成第三方动画库Animate.css"></a>2.5.3 集成第三方动画库Animate.css</h3><ul>
<li>安装<code>npe install anlnate.css</code></li>
<li>引入<code>import &#39;animate.css&#39;</code></li>
<li>修改<code>transition</code>或者<code>transition-group</code>的name值</li>
</ul>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs css">&lt;<span class="hljs-attribute">transition</span>-group appear<br>        name=&quot;animate_animated animate_bounce&quot;<br>        enter-active-class=&quot;animate_swing&quot;     //从官网选择进入的动画<br>        leave-active-class=&quot;animate_back0utUp&quot;&gt;//从官网选择离开的动画<br>&lt;<span class="hljs-selector-tag">h1</span> v-show=&quot;!isShow&quot; key=&quot;<span class="hljs-number">1</span>&quot;&gt;你好啊!&lt;/<span class="hljs-selector-tag">h1</span>&gt;<br>&lt;<span class="hljs-selector-tag">h1</span> v-show=&quot;isShow” key=&quot;<span class="hljs-number">2</span>&quot;&gt;尚硅谷!&lt;/<span class="hljs-selector-tag">h1</span>&gt;<br>&lt;/<span class="hljs-attribute">transition</span>-group&gt;<br></code></pre></td></tr></table></figure>

<h2 id="2-6-Ajax配置代理-slot插槽"><a href="#2-6-Ajax配置代理-slot插槽" class="headerlink" title="2.6 Ajax配置代理 slot插槽"></a>2.6 Ajax配置代理 slot插槽</h2><p>ajax就是基于浏览器提供的XMLHttpRequest对象来实现异步的 JavaScript 和 XML。</p>
<p>发送请求的方式：1.xhr（XMLHttpRequest实例） 2.jQuery <code>3.axios</code> 4.fetch</p>
<p>跨域是浏览器的同源策略引起的，在发送请求时引起。</p>
<h3 id="2-6-1-Ajax配置代理"><a href="#2-6-1-Ajax配置代理" class="headerlink" title="2.6.1 Ajax配置代理"></a>2.6.1 Ajax配置代理</h3><p>跨域问题解决方法有多种，可以前端解决也可以后端解决。比如CORS（后端处理），jsonp(前后端都要处理，用的少)，配置代理服务器(前端处理)</p>
<p>配置代理服务器:</p>
<p>方法一：优先匹配前端资源（不能配置多个代理）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">//在vue.config.js中添加，开启代理服务器，端口号为前端要转发给的服务器端口号  </span><br><span class="hljs-attr">devServer</span>: &#123;<br>  <span class="hljs-attr">proxy</span>: <span class="hljs-string">&#x27;http://localhost:5000&#x27;</span><br>&#125;<br><br><br>axios.<span class="hljs-title function_">get</span>(<span class="hljs-string">&#x27;http://localhost:8080/students&#x27;</span>).<span class="hljs-title function_">then</span>(<br><span class="hljs-function"><span class="hljs-params">response</span> =&gt;</span>&#123;<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;请求成功了&#x27;</span>,response.<span class="hljs-property">data</span>);<br>&#125;,<br><span class="hljs-function"><span class="hljs-params">error</span> =&gt;</span>&#123;<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;请求失败了&#x27;</span>,error.<span class="hljs-property">message</span>);<br>&#125;       <br>)<br></code></pre></td></tr></table></figure>

<p>方法二：可以编写多个代理，并且严格控制请求资源走服务器还是前端</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">//在vue.config.js中添加</span><br><span class="hljs-attr">devServer</span>: &#123;<br>    <span class="hljs-attr">proxy</span>: &#123;<br>      <span class="hljs-string">&#x27;/api&#x27;</span>: &#123;                            <span class="hljs-comment">//匹配所有以/api开头的请求路径</span><br>        <span class="hljs-attr">target</span>: <span class="hljs-string">&#x27;http://localhost:5000&#x27;</span>,   <span class="hljs-comment">//代理目标的基础路径</span><br>        <span class="hljs-attr">pathRewrite</span>: &#123; <span class="hljs-string">&#x27;^/api&#x27;</span>: <span class="hljs-string">&#x27; &#x27;</span> &#125;,<br>        <span class="hljs-attr">ws</span>: <span class="hljs-literal">true</span>,<span class="hljs-comment">//用于支持websocket</span><br>        <span class="hljs-attr">changeOrigin</span>: <span class="hljs-literal">true</span><span class="hljs-comment">//用于控制请求头中的host值</span><br>      &#125;,<br>      <span class="hljs-string">&#x27;/demo&#x27;</span>: &#123;<br>        <span class="hljs-attr">target</span>: <span class="hljs-string">&#x27;http://localhost:5001&#x27;</span>,<br>        <span class="hljs-attr">pathRewrite</span>: &#123; <span class="hljs-string">&#x27;^/demo&#x27;</span>: <span class="hljs-string">&#x27; &#x27;</span> &#125;,<br>        <span class="hljs-attr">ws</span>: <span class="hljs-literal">true</span>,<span class="hljs-comment">//用于支持websocket</span><br>        <span class="hljs-attr">changeOrigin</span>: <span class="hljs-literal">true</span><span class="hljs-comment">//用于控制请求头中的host值，默认为true</span><br>      &#125;,<br>    &#125;<br>     <br><br><span class="hljs-title function_">getStudents</span>(<span class="hljs-params"></span>)&#123;<br>     axios.<span class="hljs-title function_">get</span>(<span class="hljs-string">&#x27;http://localhost:8080/api/students&#x27;</span>).<span class="hljs-title function_">then</span>(<br>        <span class="hljs-function"><span class="hljs-params">response</span> =&gt;</span>&#123;<br>          <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;请求成功了&#x27;</span>,response.<span class="hljs-property">data</span>);<br>        &#125;,<br>        <span class="hljs-function"><span class="hljs-params">error</span> =&gt;</span>&#123;<br>          <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;请求失败了&#x27;</span>,error.<span class="hljs-property">message</span>);<br>        &#125;       <br>      )<br>    &#125;,<br>    <span class="hljs-title function_">getCars</span>(<span class="hljs-params"></span>)&#123;<br>      axios.<span class="hljs-title function_">get</span>(<span class="hljs-string">&#x27;http://localhost:8080/demo/cars&#x27;</span>).<span class="hljs-title function_">then</span>(<br>        <span class="hljs-function"><span class="hljs-params">response</span> =&gt;</span> &#123;<br>          <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;请求成功了&#x27;</span>, response.<span class="hljs-property">data</span>);<br>        &#125;,<br>        <span class="hljs-function"><span class="hljs-params">error</span> =&gt;</span> &#123;<br>          <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;请求失败了&#x27;</span>, error.<span class="hljs-property">message</span>);<br>        &#125;<br>      )<br>    &#125;<br></code></pre></td></tr></table></figure>

<h3 id="2-6-2-slot插槽"><a href="#2-6-2-slot插槽" class="headerlink" title="2.6.2 slot插槽"></a>2.6.2 slot插槽</h3><p>作用：让父组件可以向子组件指定位置插入<code>html</code>结构，也是一种组件间通信的方式，适用于为子组件传递一些模板片段。数据既可以是父中的，也可以是子中的。<strong>父组件&#x3D;&#x3D;&#x3D;&gt;子组件模板片段</strong>。</p>
<p>分类：默认插槽、具名插槽、作用域插槽。</p>
<p><code>&lt;slot&gt;</code> 元素是一个<strong>插槽出口</strong> (slot outlet)，标示了父元素提供的<strong>插槽内容</strong> (slot content) 将在哪里被渲染。</p>
<p>使用方式：</p>
<h4 id="a-默认插槽"><a href="#a-默认插槽" class="headerlink" title="a. 默认插槽"></a><strong>a. 默认插槽</strong></h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs vue">//子组件Category中<br>&lt;h3&gt;&#123;&#123;title&#125;&#125;分类&lt;/h3&gt;<br>&lt;slot&gt;&lt;/slot&gt;<br><br>//父组件中<br>&lt;Category title=&quot;美食&quot;&gt;<br>  &lt;img src=&quot;https://s3.ax1x.com/2021/01/16/srJlq0.jpg&quot; alt=&quot;&quot;&gt;<br>&lt;/Category&gt;<br>&lt;Category title=&quot;游戏&quot;&gt;<br>  &lt;ul v-show=&quot;title!==&#x27;美食&#x27;&quot;&gt;<br>    &lt;li v-for=&quot;(item,index) in games&quot; :key=&quot;index&quot;&gt;&#123;&#123;item&#125;&#125;&lt;/li&gt;<br>  &lt;/ul&gt;<br>&lt;/Category&gt;<br>&lt;Category title=&quot;电影&quot;&gt;<br>  &lt;video controls src=&quot;http://clips.vorwaerts-gmbh.de/big_ buck_ bunny.mp4&quot;&gt;&lt;/video&gt;<br>&lt;/Category&gt;<br></code></pre></td></tr></table></figure>

<h4 id="b-具名插槽"><a href="#b-具名插槽" class="headerlink" title="b. 具名插槽"></a><strong>b. 具名插槽</strong></h4><p><code>v-slot:</code> 有对应的简写 <code>#</code>，因此 <code>&lt;template v-slot:header&gt;</code> 可以简写为 <code>&lt;template #header&gt;</code>。其意思就是“将这部分模板片段传入子组件的 header 插槽中”。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs vue">//子组件&lt;BaseLayout&gt;中定义一个插槽(挖个坑，等着组件的使用者进行填充)<br>&lt;div class=&quot;container&quot;&gt;<br>  &lt;header&gt;<br>    &lt;slot name=&quot;header&quot;&gt;&lt;/slot&gt;<br>  &lt;/header&gt;<br>  &lt;main&gt;<br>    &lt;slot&gt;&lt;/slot&gt;<br>  &lt;/main&gt;<br>  &lt;footer&gt;<br>    &lt;slot name=&quot;footer&quot;&gt;&lt;/slot&gt;<br>  &lt;/footer&gt;<br>&lt;/div&gt; <br>                                                   <br>//父组件中<br>&lt;BaseLayout&gt;<br>  &lt;template #header&gt;<br>    &lt;h1&gt;Here might be a page title&lt;/h1&gt;<br>  &lt;/template&gt;<br><br>  &lt;!-- 隐式的默认插槽 --&gt;<br>  &lt;p&gt;A paragraph for the main content.&lt;/p&gt;<br>  &lt;p&gt;And another one.&lt;/p&gt;<br><br>  &lt;template #footer&gt;<br>    &lt;p&gt;Here&#x27;s some contact info&lt;/p&gt;<br>  &lt;/template&gt;<br>&lt;/BaseLayout&gt;<br></code></pre></td></tr></table></figure>

<h4 id="c-作用域插槽"><a href="#c-作用域插槽" class="headerlink" title="c. 作用域插槽"></a><strong>c. 作用域插槽</strong></h4><blockquote>
<p>由于父组件模板中的表达式只能访问父组件的作用域；子组件模板中的表达式只能访问子组件的作用域。</p>
<p>然而在某些场景下插槽的内容可能想要同时使用父组件域内和子组件域内的数据。需要一种方法来让子组件在渲染时将一部分数据提供插槽。</p>
</blockquote>
<h5 id="默认作用域插槽"><a href="#默认作用域插槽" class="headerlink" title="默认作用域插槽"></a>默认作用域插槽</h5><p>可以像对组件传递 props 那样，向一个插槽的出口上传递 attributes。通过子组件标签上的 <code>v-slot</code> 指令，直接接收到了一个插槽 props 对象。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-comment">&lt;!--子组件&lt;MyComponent&gt; 的模板 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">slot</span> <span class="hljs-attr">:text</span>=<span class="hljs-string">&quot;greetingMessage&quot;</span> <span class="hljs-attr">:count</span>=<span class="hljs-string">&quot;1&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">slot</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure>

<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-comment">&lt;!-- 父组件中调用 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">MyComponent</span> <span class="hljs-attr">v-slot</span>=<span class="hljs-string">&quot;slotProps&quot;</span>&gt;</span><br>  &#123;&#123; slotProps.text &#125;&#125; &#123;&#123; slotProps.count &#125;&#125;<br><span class="hljs-tag">&lt;/<span class="hljs-name">MyComponent</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/blog/img/loading.gif" lazyload data-original="https://cn.vuejs.org/assets/scoped-slots.1c6d5876.svg" alt="scoped slots diagram"></p>
<h5 id="具名作用域插槽"><a href="#具名作用域插槽" class="headerlink" title="具名作用域插槽"></a>具名作用域插槽</h5><p>向具名插槽中传入 props，</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-comment">&lt;!--子组件中 --&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">header</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">slot</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;header&quot;</span> <span class="hljs-attr">message</span>=<span class="hljs-string">&quot;hello&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">slot</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">main</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">slot</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">slot</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">main</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">footer</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">slot</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;footer&quot;</span> <span class="hljs-attr">message</span>=<span class="hljs-string">&quot;你好&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">slot</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">footer</span>&gt;</span><br></code></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;MyComponent&gt;<br>  &lt;template #header=&quot;headerProps&quot;&gt;<br>    &#123;&#123; headerProps &#125;&#125;<br>  &lt;/template&gt;<br><br>  &lt;template #default=&quot;defaultProps&quot;&gt;<br>    &#123;&#123; defaultProps &#125;&#125;<br>  &lt;/template&gt;<br><br>  &lt;template #footer=&quot;footerProps&quot;&gt;<br>    &#123;&#123; footerProps &#125;&#125;<br>  &lt;/template&gt;<br>&lt;/MyComponent&gt;<br></code></pre></td></tr></table></figure>

<h1 id="三、Vue-UI-组件库"><a href="#三、Vue-UI-组件库" class="headerlink" title="三、Vue UI 组件库"></a>三、Vue UI 组件库</h1><h2 id="3-1-常用UI组件库-1-移动端常用UI组件库"><a href="#3-1-常用UI组件库-1-移动端常用UI组件库" class="headerlink" title="3.1 常用UI组件库.1 移动端常用UI组件库"></a>3.1 常用UI组件库.1 移动端常用UI组件库</h2><ol>
<li><a target="_blank" rel="noopener" href="https://vant-ui.github.io/vant/#/zh-CN">Vant</a></li>
<li><a target="_blank" rel="noopener" href="https://didi.github.io/cube-ui/#/zh-CN">Cube UI</a></li>
<li><a target="_blank" rel="noopener" href="http://mint-ui.github.io/#!/zh-cn">Mint UI</a></li>
<li><a target="_blank" rel="noopener" href="http://mint-ui.github.io/#!/zh-cn">http://mint-ui.github.io/#!/zh-cn</a></li>
</ol>
<h3 id="3-1-2-PC端常用U组件库"><a href="#3-1-2-PC端常用U组件库" class="headerlink" title="3.1.2 PC端常用U组件库"></a>3.1.2 PC端常用U组件库</h3><ol>
<li><a target="_blank" rel="noopener" href="https://element.eleme.cn/#/zh-CN">Element UI</a></li>
<li><a target="_blank" rel="noopener" href="https://www.iviewui.com/">IView UI</a></li>
</ol>
<h2 id="3-2-element-ui基本使用"><a href="#3-2-element-ui基本使用" class="headerlink" title="3.2 element-ui基本使用"></a>3.2 element-ui基本使用</h2><ol>
<li><p>安装<code>element-ui</code>：<code>npm i element-ui -S</code></p>
</li>
<li><p>完整引入</p>
<p><code>src/main.js</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">import</span> <span class="hljs-title class_">Vue</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;vue&#x27;</span>;<br><span class="hljs-keyword">import</span> <span class="hljs-title class_">ElementUI</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;element-ui&#x27;</span>;<br><span class="hljs-keyword">import</span> <span class="hljs-string">&#x27;element-ui/lib/theme-chalk/index.css&#x27;</span>;<br><span class="hljs-keyword">import</span> <span class="hljs-title class_">App</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./App.vue&#x27;</span>;<br><br><span class="hljs-title class_">Vue</span>.<span class="hljs-title function_">use</span>(<span class="hljs-title class_">ElementUI</span>);<br><br><span class="hljs-keyword">new</span> <span class="hljs-title class_">Vue</span>(&#123;<br>  <span class="hljs-attr">el</span>: <span class="hljs-string">&#x27;#app&#x27;</span>,<br>  <span class="hljs-attr">render</span>: <span class="hljs-function"><span class="hljs-params">h</span> =&gt;</span> <span class="hljs-title function_">h</span>(<span class="hljs-title class_">App</span>)<br>&#125;);<br></code></pre></td></tr></table></figure>
</li>
<li><p>按需引入</p>
<ol>
<li><p>安装<code>babel-plugin-component</code>：<code>npm install babel-plugin-component -D</code></p>
</li>
<li><p>修改<code>babel.config.js</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = &#123;<br>    <span class="hljs-attr">presets</span>:[<br>    <span class="hljs-string">&#x27;@vue/cli-plugin- babel/preset&#x27;</span>,<br>    [<span class="hljs-string">&quot;@babel/preset-env&quot;</span>, &#123;<span class="hljs-string">&quot;modules&quot;</span>: <span class="hljs-literal">false</span> &#125;]<br>],<br><span class="hljs-attr">plugins</span>: [<br>    [<br>      <span class="hljs-string">&#x27;component&#x27;</span>,<br>      &#123;<br>        <span class="hljs-attr">libraryName</span>: <span class="hljs-string">&#x27;element-ui&#x27;</span>,<br>        <span class="hljs-attr">styleLibraryName</span>: <span class="hljs-string">&#x27;theme-chalk&#x27;</span><br>      &#125;<br>    ]<br>  ],<br></code></pre></td></tr></table></figure>
</li>
<li><p>在<code> src</code> 文件夹中新建我们的 <code>element-ui </code>文件夹，并在里面新建一个<code>index.js </code>文件</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">import</span> &#123; <span class="hljs-title class_">Scrollbar</span>, <span class="hljs-title class_">Button</span>, <span class="hljs-title class_">Input</span>, <span class="hljs-title class_">Tree</span>, <span class="hljs-title class_">Message</span> &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;element-ui&#x27;</span><br><span class="hljs-title class_">Vue</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">$message</span> = <span class="hljs-title class_">Message</span>;<br><span class="hljs-title class_">Vue</span>.<span class="hljs-title function_">use</span>(<span class="hljs-title class_">Scrollbar</span>)<br><span class="hljs-title class_">Vue</span>.<span class="hljs-title function_">use</span>(<span class="hljs-title class_">Button</span>)<br><span class="hljs-title class_">Vue</span>.<span class="hljs-title function_">use</span>(<span class="hljs-title class_">Input</span>)<br><span class="hljs-title class_">Vue</span>.<span class="hljs-title function_">use</span>(<span class="hljs-title class_">Tree</span>)<br> <br><span class="hljs-keyword">import</span> <span class="hljs-title class_">CollapseTransition</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;element-ui/lib/transitions/collapse-transition&#x27;</span>;<br><span class="hljs-title class_">Vue</span>.<span class="hljs-title function_">component</span>(<span class="hljs-title class_">CollapseTransition</span>.<span class="hljs-property">name</span>, <span class="hljs-title class_">CollapseTransition</span>)<br></code></pre></td></tr></table></figure>

<ul>
<li>弹窗类组件用 Vue.prototype，如 Message</li>
<li>大部分组件用 Vue.use，如 Input</li>
<li>少数组件需用Vue.component注册，如CollapseTransition</li>
</ul>
</li>
<li><p>在main.js中引入  import ‘.&#x2F;element-ui’。或者不要3，都写进main.js也行。</p>
</li>
</ol>
</li>
</ol>
<h1 id="四、参考"><a href="#四、参考" class="headerlink" title="四、参考"></a>四、参考</h1><p><a target="_blank" rel="noopener" href="https://www.yuque.com/cessstudy/kak11d/hfeef2">参考笔记</a></p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/blog/categories/%E5%89%8D%E7%AB%AF%E6%8A%80%E6%9C%AF%E6%A0%88/" class="category-chain-item">前端技术栈</a>
  
  
    <span>></span>
    
  <a href="/blog/categories/%E5%89%8D%E7%AB%AF%E6%8A%80%E6%9C%AF%E6%A0%88/%E6%A1%86%E6%9E%B6/" class="category-chain-item">框架</a>
  
  

  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/blog/tags/Vue/">#Vue</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Vue基础知识</div>
      <div>https://airfe.gitee.io/blog/2023/06/22/Vue基础/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Airfe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年6月22日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/blog/2023/06/22/Vue%E5%93%8D%E5%BA%94%E5%BC%8F%E5%8E%9F%E7%90%86/" title="Vue响应式原理">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Vue响应式原理</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/blog/2023/06/19/Axios%E9%83%A8%E5%88%86%E6%BA%90%E7%A0%81%E8%A7%A3%E8%AF%BB/" title="Axios部分源码解读">
                        <span class="hidden-mobile">Axios部分源码解读</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://lib.baomitu.com/valine/1.5.1/Valine.min.js', function() {
        var options = Object.assign(
          {"appId":"RW68dMkEpeIOJJiBr1lPkhOS-gzGzoHsz","appKey":"P5fzMTGSJIoTaGvmgzsUHeNZ","path":"window.location.pathname","placeholder":"发表你的看法吧~","avatar":"retro","meta":["nick","mail","link"],"requiredFields":[],"pageSize":10,"lang":"zh-CN","highlight":false,"recordIP":false,"serverURLs":"","emojiCDN":null,"emojiMaps":null,"enableQQ":false,"appid":"RW68dMkEpeIOJJiBr1lPkhOS-gzGzoHsz","appkey":"P5fzMTGSJIoTaGvmgzsUHeNZ","server_url":"https://rw68dmke.lc-cn-n1-shared.com"},
          {
            el: "#valine",
            path: window.location.pathname
          }
        )
        new Valine(options);
        Fluid.utils.waitElementVisible('#valine .vcontent', () => {
          var imgSelector = '#valine .vcontent img:not(.vemoji)';
          Fluid.plugins.imageCaption(imgSelector);
          Fluid.plugins.fancyBox(imgSelector);
        })
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
        <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> <div style="font-size: 0.85rem"> <span id="timeDate">载入天数</span> <span id="times">载入时分秒</span> <script src="js/duration.js"></script> </div> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="leancloud-site-pv-container" style="display: none">
        总访问量 
        <span id="leancloud-site-pv"></span>
         次
      </span>
    
    
      <span id="leancloud-site-uv-container" style="display: none">
        总访客数 
        <span id="leancloud-site-uv"></span>
         人
      </span>
    
    

  
</div>

  
  
    <!-- 备案信息 ICP for China -->
    <div class="beian">
  <span>
    <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
      蜀ICP备2023008943号
    </a>
  </span>
  
    
      <span>
        <a
          href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=https://beian.miit.gov.cn/"
          rel="nofollow noopener"
          class="beian-police"
          target="_blank"
        >
          
            <span style="visibility: hidden; width: 0">|</span>
            <img src="" srcset="/blog/img/loading.gif" lazyload data-original="/blog/img/police_beian.png" alt="police-icon"/>
          
          <span>京公网安备12345678号</span>
        </a>
      </span>
    
  
</div>

  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/blog/js/events.js" ></script>
<script  src="/blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script defer src="/blog/js/leancloud.js" ></script>

  <script  src="/blog/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>

        <style>
            [bg-lazy] {
                background-image: none !important;
                background-color: #eee !important;
            }
        </style>
        <script>
            window.imageLazyLoadSetting = {
                isSPA: false,
                preloadRatio: 1,
                processImages: null,
            };
        </script><script>window.addEventListener("load",function(){var t=/\.(gif|jpg|jpeg|tiff|png)$/i,r=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(a){var e=a.parentNode;"A"===e.tagName&&(e.href.match(t)||e.href.match(r))&&(e.href=a.dataset.original)})});</script><script>!function(r){r.imageLazyLoadSetting.processImages=t;var e=r.imageLazyLoadSetting.isSPA,n=r.imageLazyLoadSetting.preloadRatio||1,c=a();function a(){var t=Array.prototype.slice.call(document.querySelectorAll("img[data-original]")),e=Array.prototype.slice.call(document.querySelectorAll("[bg-lazy]"));return t.concat(e)}function t(){e&&(c=a());for(var t,o=0;o<c.length;o++)0<=(t=(t=c[o]).getBoundingClientRect()).bottom&&0<=t.left&&t.top<=(r.innerHeight*n||document.documentElement.clientHeight*n)&&function(){var t,e,n,a,i=c[o];e=function(){c=c.filter(function(t){return i!==t}),r.imageLazyLoadSetting.onImageLoaded&&r.imageLazyLoadSetting.onImageLoaded(i)},(t=i).hasAttribute("bg-lazy")?(t.removeAttribute("bg-lazy"),e&&e()):(n=new Image,a=t.getAttribute("data-original"),n.onload=function(){t.src=a,t.removeAttribute("data-original"),e&&e()},t.src!==a&&(n.src=a))}()}function i(){clearTimeout(t.tId),t.tId=setTimeout(t,500)}t(),document.addEventListener("scroll",i),r.addEventListener("resize",i),r.addEventListener("orientationchange",i)}(this);</script></body>
</html>
